blob: 81a57b222fd3777551d4cd61e3a91a802acf193f [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"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000036using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000037
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000038namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000039 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000040 C,
41 Cpp,
42 ObjC
43 };
44}
45
Chris Lattner58418ff2008-06-29 00:16:31 +000046//===----------------------------------------------------------------------===//
47// Helper functions
48//===----------------------------------------------------------------------===//
49
Ted Kremenek527042b2009-08-14 20:49:40 +000050/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000051/// type (function or function-typed variable) or an Objective-C
52/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000053static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000054 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000055}
David Majnemer06864812015-04-07 06:01:53 +000056/// \brief Return true if the given decl has function type (function or
57/// function-typed variable) or an Objective-C method or a block.
58static bool isFunctionOrMethodOrBlock(const Decl *D) {
59 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
60}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000061
John McCall3882ace2011-01-05 12:14:39 +000062/// Return true if the given decl has a declarator that should have
63/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000064static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000065 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000066 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
67 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000068}
69
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000070/// hasFunctionProto - Return true if the given decl has a argument
71/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000072/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000073static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000074 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000075 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000076 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000077}
78
Alp Toker601b22c2014-01-21 23:35:24 +000079/// getFunctionOrMethodNumParams - Return number of function or method
80/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000081/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000082static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000083 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000084 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000085 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000086 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000087 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000088}
89
Alp Toker601b22c2014-01-21 23:35:24 +000090static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000091 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000092 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000094 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000095
Alp Toker03376dc2014-07-07 09:02:20 +000096 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000097}
98
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000099static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
100 if (const auto *FD = dyn_cast<FunctionDecl>(D))
101 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000102 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000103 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000104 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000105 return BD->getParamDecl(Idx)->getSourceRange();
106 return SourceRange();
107}
108
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000109static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000110 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000111 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000112 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000113}
114
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000115static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
116 if (const auto *FD = dyn_cast<FunctionDecl>(D))
117 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000118 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000119 return MD->getReturnTypeSourceRange();
120 return SourceRange();
121}
122
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000123static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000124 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000125 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000126 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000127 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
129 return BD->isVariadic();
130
131 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000132}
133
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134static bool isInstanceMethod(const Decl *D) {
135 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000136 return MethodDecl->isInstance();
137 return false;
138}
139
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000140static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000141 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000142 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000143 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000144
John McCall96fa4842010-05-17 21:00:27 +0000145 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
146 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000147 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000148
John McCall96fa4842010-05-17 21:00:27 +0000149 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000150
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000151 // FIXME: Should we walk the chain of classes?
152 return ClsName == &Ctx.Idents.get("NSString") ||
153 ClsName == &Ctx.Idents.get("NSMutableString");
154}
155
Daniel Dunbar980c6692008-09-26 03:32:58 +0000156static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000157 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000158 if (!PT)
159 return false;
160
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000161 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000162 if (!RT)
163 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000164
Daniel Dunbar980c6692008-09-26 03:32:58 +0000165 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000166 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000167 return false;
168
169 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
170}
171
Richard Smithb87c4652013-10-31 21:23:20 +0000172static unsigned getNumAttributeArgs(const AttributeList &Attr) {
173 // FIXME: Include the type in the argument list.
174 return Attr.getNumArgs() + Attr.hasParsedType();
175}
176
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000177template <typename Compare>
178static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
179 unsigned Num, unsigned Diag,
180 Compare Comp) {
181 if (Comp(getNumAttributeArgs(Attr), Num)) {
182 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000183 return false;
184 }
185
186 return true;
187}
188
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000189/// \brief Check if the attribute has exactly as many args as Num. May
190/// output an error.
191static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
192 unsigned Num) {
193 return checkAttributeNumArgsImpl(S, Attr, Num,
194 diag::err_attribute_wrong_number_arguments,
195 std::not_equal_to<unsigned>());
196}
197
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000198/// \brief Check if the attribute has at least as many args as Num. May
199/// output an error.
200static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000201 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000202 return checkAttributeNumArgsImpl(S, Attr, Num,
203 diag::err_attribute_too_few_arguments,
204 std::less<unsigned>());
205}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000206
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000207/// \brief Check if the attribute has at most as many args as Num. May
208/// output an error.
209static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
210 unsigned Num) {
211 return checkAttributeNumArgsImpl(S, Attr, Num,
212 diag::err_attribute_too_many_arguments,
213 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000214}
215
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000216/// \brief If Expr is a valid integer constant, get the value of the integer
217/// expression and return success or failure. May output an error.
218static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
219 const Expr *Expr, uint32_t &Val,
220 unsigned Idx = UINT_MAX) {
221 llvm::APSInt I(32);
222 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
223 !Expr->isIntegerConstantExpr(I, S.Context)) {
224 if (Idx != UINT_MAX)
225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
226 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
227 << Expr->getSourceRange();
228 else
229 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
230 << Attr.getName() << AANT_ArgumentIntegerConstant
231 << Expr->getSourceRange();
232 return false;
233 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000234
235 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000236 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
237 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000238 return false;
239 }
240
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000241 Val = (uint32_t)I.getZExtValue();
242 return true;
243}
244
Aaron Ballmanfb763042013-12-02 18:05:46 +0000245/// \brief Diagnose mutually exclusive attributes when present on a given
246/// declaration. Returns true if diagnosed.
247template <typename AttrTy>
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000248static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range,
249 IdentifierInfo *Ident) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000250 if (AttrTy *A = D->getAttr<AttrTy>()) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000251 S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident
252 << A;
253 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000254 return true;
255 }
256 return false;
257}
258
Alp Toker601b22c2014-01-21 23:35:24 +0000259/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000260/// instance method D. May output an error.
261///
262/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000263static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
264 const AttributeList &Attr,
265 unsigned AttrArgNum,
266 const Expr *IdxExpr,
267 uint64_t &Idx) {
David Majnemer06864812015-04-07 06:01:53 +0000268 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000269
270 // In C++ the implicit 'this' function parameter also counts.
271 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000272 bool HP = hasFunctionProto(D);
273 bool HasImplicitThisParam = isInstanceMethod(D);
274 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000275 unsigned NumParams =
276 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000277
278 llvm::APSInt IdxInt;
279 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
280 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
282 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
283 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000284 return false;
285 }
286
287 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000288 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000289 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
290 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000291 return false;
292 }
293 Idx--; // Convert to zero-based.
294 if (HasImplicitThisParam) {
295 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000296 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000297 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000298 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000299 return false;
300 }
301 --Idx;
302 }
303
304 return true;
305}
306
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000307/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
308/// If not emit an error and return false. If the argument is an identifier it
309/// will emit an error with a fixit hint and treat it as if it was a string
310/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000311bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
312 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000313 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000314 // Look for identifiers. If we have one emit a hint to fix it to a literal.
315 if (Attr.isArgIdent(ArgNum)) {
316 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000317 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000318 << Attr.getName() << AANT_ArgumentString
319 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000320 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000321 Str = Loc->Ident->getName();
322 if (ArgLocation)
323 *ArgLocation = Loc->Loc;
324 return true;
325 }
326
327 // Now check for an actual string literal.
328 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
329 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
330 if (ArgLocation)
331 *ArgLocation = ArgExpr->getLocStart();
332
333 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000334 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000335 << Attr.getName() << AANT_ArgumentString;
336 return false;
337 }
338
339 Str = Literal->getString();
340 return true;
341}
342
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000343/// \brief Applies the given attribute to the Decl without performing any
344/// additional semantic checking.
345template <typename AttrType>
346static void handleSimpleAttribute(Sema &S, Decl *D,
347 const AttributeList &Attr) {
348 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
349 Attr.getAttributeSpellingListIndex()));
350}
351
Justin Lebar3eaaf862016-01-13 01:07:35 +0000352template <typename AttrType>
353static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
354 const AttributeList &Attr) {
355 handleSimpleAttribute<AttrType>(S, D, Attr);
356}
357
358/// \brief Applies the given attribute to the Decl so long as the Decl doesn't
359/// already have one of the given incompatible attributes.
360template <typename AttrType, typename IncompatibleAttrType,
361 typename... IncompatibleAttrTypes>
362static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
363 const AttributeList &Attr) {
364 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, Attr.getRange(),
365 Attr.getName()))
366 return;
367 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
368 Attr);
369}
370
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000371/// \brief Check if the passed-in expression is of type int or bool.
372static bool isIntOrBool(Expr *Exp) {
373 QualType QT = Exp->getType();
374 return QT->isBooleanType() || QT->isIntegerType();
375}
376
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000377
378// Check to see if the type is a smart pointer of some kind. We assume
379// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000380static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000381 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
382 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000383 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000384 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000385
Richard Smithcf4bdde2015-02-21 02:45:19 +0000386 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
387 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000388 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000389 return false;
390
391 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000392}
393
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000394/// \brief Check if passed in Decl is a pointer type.
395/// Note that this function may produce an error message.
396/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000397static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
398 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000399 const ValueDecl *vd = cast<ValueDecl>(D);
400 QualType QT = vd->getType();
401 if (QT->isAnyPointerType())
402 return true;
403
404 if (const RecordType *RT = QT->getAs<RecordType>()) {
405 // If it's an incomplete type, it could be a smart pointer; skip it.
406 // (We don't want to force template instantiation if we can avoid it,
407 // since that would alter the order in which templates are instantiated.)
408 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000409 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000410
Aaron Ballman553e6812013-12-26 14:54:11 +0000411 if (threadSafetyCheckIsSmartPointer(S, RT))
412 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000413 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000414
415 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000416 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000417 return false;
418}
419
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000420/// \brief Checks that the passed in QualType either is of RecordType or points
421/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000422static const RecordType *getRecordType(QualType QT) {
423 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000424 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000425
426 // Now check if we point to record type.
427 if (const PointerType *PT = QT->getAs<PointerType>())
428 return PT->getPointeeType()->getAs<RecordType>();
429
Craig Topperc3ec1492014-05-26 06:22:03 +0000430 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000431}
432
Aaron Ballman76050722014-04-04 15:13:57 +0000433static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000434 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000435
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000436 if (!RT)
437 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000438
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000439 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000440 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000441 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000442
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000443 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000444 // FIXME -- Check the type that the smart pointer points to.
445 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000446 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000447
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000448 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000449 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000450 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000451 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000452
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000453 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000454 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
455 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000456 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
457 const auto *Type = BS->getType()->getAs<RecordType>();
458 return Type->getDecl()->hasAttr<CapabilityAttr>();
459 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000460 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000461 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000462 return false;
463}
464
Aaron Ballman76050722014-04-04 15:13:57 +0000465static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000466 const auto *TD = Ty->getAs<TypedefType>();
467 if (!TD)
468 return false;
469
470 TypedefNameDecl *TN = TD->getDecl();
471 if (!TN)
472 return false;
473
474 return TN->hasAttr<CapabilityAttr>();
475}
476
Aaron Ballman76050722014-04-04 15:13:57 +0000477static bool typeHasCapability(Sema &S, QualType Ty) {
478 if (checkTypedefTypeForCapability(Ty))
479 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000480
Aaron Ballman76050722014-04-04 15:13:57 +0000481 if (checkRecordTypeForCapability(S, Ty))
482 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000483
Aaron Ballman76050722014-04-04 15:13:57 +0000484 return false;
485}
486
487static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
488 // Capability expressions are simple expressions involving the boolean logic
489 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
490 // a DeclRefExpr is found, its type should be checked to determine whether it
491 // is a capability or not.
492
493 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
494 return typeHasCapability(S, E->getType());
495 else if (const auto *E = dyn_cast<CastExpr>(Ex))
496 return isCapabilityExpr(S, E->getSubExpr());
497 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
498 return isCapabilityExpr(S, E->getSubExpr());
499 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
500 if (E->getOpcode() == UO_LNot)
501 return isCapabilityExpr(S, E->getSubExpr());
502 return false;
503 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
504 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
505 return isCapabilityExpr(S, E->getLHS()) &&
506 isCapabilityExpr(S, E->getRHS());
507 return false;
508 }
509
510 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000511}
512
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000513/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
514/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000515/// \param Sidx The attribute argument index to start checking with.
516/// \param ParamIdxOk Whether an argument can be indexing into a function
517/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000518static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
519 const AttributeList &Attr,
520 SmallVectorImpl<Expr *> &Args,
521 int Sidx = 0,
522 bool ParamIdxOk = false) {
523 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000524 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000525
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000526 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000527 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000528 Args.push_back(ArgExp);
529 continue;
530 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000531
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000532 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000533 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000534 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000535 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000536 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000537 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000538 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000539 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000540
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000541 // We allow constant strings to be used as a placeholder for expressions
542 // that are not valid C++ syntax, but warn that they are ignored.
543 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
544 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000545 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000546 continue;
547 }
548
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000549 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000550
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000551 // A pointer to member expression of the form &MyClass::mu is treated
552 // specially -- we need to look at the type of the member.
553 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
554 if (UOp->getOpcode() == UO_AddrOf)
555 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
556 if (DRE->getDecl()->isCXXInstanceMember())
557 ArgTy = DRE->getDecl()->getType();
558
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000559 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000560 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000561
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000562 // Now check if we index into a record type function param.
563 if(!RT && ParamIdxOk) {
564 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000565 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
566 if(FD && IL) {
567 unsigned int NumParams = FD->getNumParams();
568 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000569 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
570 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
571 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000572 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
573 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000574 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000575 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000576 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000577 }
578 }
579
Aaron Ballman76050722014-04-04 15:13:57 +0000580 // If the type does not have a capability, see if the components of the
581 // expression have capabilities. This allows for writing C code where the
582 // capability may be on the type, and the expression is a capability
583 // boolean logic expression. Eg) requires_capability(A || B && !C)
584 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
585 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
586 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000587
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000588 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000589 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000590}
591
Chris Lattner58418ff2008-06-29 00:16:31 +0000592//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000593// Attribute Implementations
594//===----------------------------------------------------------------------===//
595
Michael Hana9171bc2012-08-03 17:40:43 +0000596static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000597 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000598 if (!threadSafetyCheckIsPointer(S, D, Attr))
599 return;
600
Michael Han99315932013-01-24 16:46:58 +0000601 D->addAttr(::new (S.Context)
602 PtGuardedVarAttr(Attr.getRange(), S.Context,
603 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000604}
605
Michael Hana9171bc2012-08-03 17:40:43 +0000606static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
607 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000608 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000609 SmallVector<Expr*, 1> Args;
610 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000611 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000612 unsigned Size = Args.size();
613 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000614 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000615
Michael Han3be3b442012-07-23 18:48:41 +0000616 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000617
Michael Han3be3b442012-07-23 18:48:41 +0000618 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000619}
620
Michael Han3be3b442012-07-23 18:48:41 +0000621static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000622 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000623 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
624 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000625
Aaron Ballman36a53502014-01-16 13:03:14 +0000626 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
627 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000628}
629
Michael Hana9171bc2012-08-03 17:40:43 +0000630static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000631 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000632 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000633 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
634 return;
635
636 if (!threadSafetyCheckIsPointer(S, D, Attr))
637 return;
638
639 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000640 S.Context, Arg,
641 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000642}
643
Michael Hana9171bc2012-08-03 17:40:43 +0000644static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
645 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000646 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000647 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000648 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000649
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000650 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000651 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000652 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
653 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
654 << Attr.getName();
655 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000656 }
657
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000658 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000659 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000660 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000661 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000662
Michael Han3be3b442012-07-23 18:48:41 +0000663 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000664}
665
Michael Hana9171bc2012-08-03 17:40:43 +0000666static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000667 const AttributeList &Attr) {
668 SmallVector<Expr*, 1> Args;
669 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
670 return;
671
672 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000673 D->addAttr(::new (S.Context)
674 AcquiredAfterAttr(Attr.getRange(), S.Context,
675 StartArg, Args.size(),
676 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000677}
678
Michael Hana9171bc2012-08-03 17:40:43 +0000679static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000680 const AttributeList &Attr) {
681 SmallVector<Expr*, 1> Args;
682 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
683 return;
684
685 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000686 D->addAttr(::new (S.Context)
687 AcquiredBeforeAttr(Attr.getRange(), S.Context,
688 StartArg, Args.size(),
689 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000690}
691
Michael Hana9171bc2012-08-03 17:40:43 +0000692static bool checkLockFunAttrCommon(Sema &S, Decl *D,
693 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000694 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000695 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000696 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000697 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000698
Michael Han3be3b442012-07-23 18:48:41 +0000699 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000700}
701
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000702static void handleAssertSharedLockAttr(Sema &S, Decl *D,
703 const AttributeList &Attr) {
704 SmallVector<Expr*, 1> Args;
705 if (!checkLockFunAttrCommon(S, D, Attr, Args))
706 return;
707
708 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000709 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000710 D->addAttr(::new (S.Context)
711 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
712 Attr.getAttributeSpellingListIndex()));
713}
714
715static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
716 const AttributeList &Attr) {
717 SmallVector<Expr*, 1> Args;
718 if (!checkLockFunAttrCommon(S, D, Attr, Args))
719 return;
720
721 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000722 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000723 D->addAttr(::new (S.Context)
724 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
725 StartArg, Size,
726 Attr.getAttributeSpellingListIndex()));
727}
728
729
Michael Hana9171bc2012-08-03 17:40:43 +0000730static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
731 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000732 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000733 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000734 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000735
Aaron Ballman00e99962013-08-31 01:11:41 +0000736 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000738 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000739 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000740 }
741
742 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000743 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000744
Michael Han3be3b442012-07-23 18:48:41 +0000745 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000746}
747
Michael Hana9171bc2012-08-03 17:40:43 +0000748static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000749 const AttributeList &Attr) {
750 SmallVector<Expr*, 2> Args;
751 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
752 return;
753
Michael Han99315932013-01-24 16:46:58 +0000754 D->addAttr(::new (S.Context)
755 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000756 Attr.getArgAsExpr(0),
757 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000758 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000759}
760
Michael Hana9171bc2012-08-03 17:40:43 +0000761static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000762 const AttributeList &Attr) {
763 SmallVector<Expr*, 2> Args;
764 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
765 return;
766
Nico Weber462fd1e2015-01-07 23:50:05 +0000767 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
768 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
769 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000770}
771
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000772static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000773 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000774 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000775 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000776 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000777 unsigned Size = Args.size();
778 if (Size == 0)
779 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000780
Michael Han99315932013-01-24 16:46:58 +0000781 D->addAttr(::new (S.Context)
782 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
783 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784}
785
786static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000788 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000789 return;
790
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000791 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000792 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000793 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000794 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000795 if (Size == 0)
796 return;
797 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000798
Michael Han99315932013-01-24 16:46:58 +0000799 D->addAttr(::new (S.Context)
800 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
801 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000802}
803
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000804static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
805 Expr *Cond = Attr.getArgAsExpr(0);
806 if (!Cond->isTypeDependent()) {
807 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
808 if (Converted.isInvalid())
809 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000810 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000811 }
812
813 StringRef Msg;
814 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
815 return;
816
817 SmallVector<PartialDiagnosticAt, 8> Diags;
818 if (!Cond->isValueDependent() &&
819 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
820 Diags)) {
821 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
822 for (int I = 0, N = Diags.size(); I != N; ++I)
823 S.Diag(Diags[I].first, Diags[I].second);
824 return;
825 }
826
827 D->addAttr(::new (S.Context)
828 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
829 Attr.getAttributeSpellingListIndex()));
830}
831
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000832static void handlePassObjectSizeAttr(Sema &S, Decl *D,
833 const AttributeList &Attr) {
834 if (D->hasAttr<PassObjectSizeAttr>()) {
835 S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter)
836 << Attr.getName();
837 return;
838 }
839
840 Expr *E = Attr.getArgAsExpr(0);
841 uint32_t Type;
842 if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1))
843 return;
844
845 // pass_object_size's argument is passed in as the second argument of
846 // __builtin_object_size. So, it has the same constraints as that second
847 // argument; namely, it must be in the range [0, 3].
848 if (Type > 3) {
849 S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range)
850 << Attr.getName() << 0 << 3 << E->getSourceRange();
851 return;
852 }
853
854 // pass_object_size is only supported on constant pointer parameters; as a
855 // kindness to users, we allow the parameter to be non-const for declarations.
856 // At this point, we have no clue if `D` belongs to a function declaration or
857 // definition, so we defer the constness check until later.
858 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
859 S.Diag(D->getLocStart(), diag::err_attribute_pointers_only)
860 << Attr.getName() << 1;
861 return;
862 }
863
864 D->addAttr(::new (S.Context)
865 PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type,
866 Attr.getAttributeSpellingListIndex()));
867}
868
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000869static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000870 ConsumableAttr::ConsumedState DefaultState;
871
872 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000873 IdentifierLoc *IL = Attr.getArgAsIdent(0);
874 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
875 DefaultState)) {
876 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
877 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000878 return;
879 }
David Blaikie16f76d22013-09-06 01:28:43 +0000880 } else {
881 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
882 << Attr.getName() << AANT_ArgumentIdentifier;
883 return;
884 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000885
886 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000887 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000888 Attr.getAttributeSpellingListIndex()));
889}
890
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000891
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000892static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
893 const AttributeList &Attr) {
894 ASTContext &CurrContext = S.getASTContext();
895 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
896
897 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
898 if (!RD->hasAttr<ConsumableAttr>()) {
899 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
900 RD->getNameAsString();
901
902 return false;
903 }
904 }
905
906 return true;
907}
908
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000909
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 Hutchins48a31762013-08-12 21:20:55 +0000948
DeLesley Hutchins69391772013-10-17 23:23:53 +0000949static void handleParamTypestateAttr(Sema &S, Decl *D,
950 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000951 ParamTypestateAttr::ConsumedState ParamState;
952
953 if (Attr.isArgIdent(0)) {
954 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
955 StringRef StateString = Ident->Ident->getName();
956
957 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
958 ParamState)) {
959 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
960 << Attr.getName() << StateString;
961 return;
962 }
963 } else {
964 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
965 Attr.getName() << AANT_ArgumentIdentifier;
966 return;
967 }
968
969 // FIXME: This check is currently being done in the analysis. It can be
970 // enabled here only after the parser propagates attributes at
971 // template specialization definition, not declaration.
972 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
973 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
974 //
975 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
976 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
977 // ReturnType.getAsString();
978 // return;
979 //}
980
981 D->addAttr(::new (S.Context)
982 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
983 Attr.getAttributeSpellingListIndex()));
984}
985
986
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000987static void handleReturnTypestateAttr(Sema &S, Decl *D,
988 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000989 ReturnTypestateAttr::ConsumedState ReturnState;
990
991 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000992 IdentifierLoc *IL = Attr.getArgAsIdent(0);
993 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
994 ReturnState)) {
995 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
996 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000997 return;
998 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000999 } else {
1000 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1001 Attr.getName() << AANT_ArgumentIdentifier;
1002 return;
1003 }
1004
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001005 // FIXME: This check is currently being done in the analysis. It can be
1006 // enabled here only after the parser propagates attributes at
1007 // template specialization definition, not declaration.
1008 //QualType ReturnType;
1009 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001010 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1011 // ReturnType = Param->getType();
1012 //
1013 //} else if (const CXXConstructorDecl *Constructor =
1014 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001015 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1016 //
1017 //} else {
1018 //
1019 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1020 //}
1021 //
1022 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1023 //
1024 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1025 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1026 // ReturnType.getAsString();
1027 // return;
1028 //}
1029
1030 D->addAttr(::new (S.Context)
1031 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1032 Attr.getAttributeSpellingListIndex()));
1033}
1034
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001035
1036static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001037 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1038 return;
1039
1040 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001041 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001042 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1043 StringRef Param = Ident->Ident->getName();
1044 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1045 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1046 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001047 return;
1048 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001049 } else {
1050 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1051 Attr.getName() << AANT_ArgumentIdentifier;
1052 return;
1053 }
1054
1055 D->addAttr(::new (S.Context)
1056 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1057 Attr.getAttributeSpellingListIndex()));
1058}
1059
Chris Wailes9385f9f2013-10-29 20:28:41 +00001060static void handleTestTypestateAttr(Sema &S, Decl *D,
1061 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001062 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1063 return;
1064
Chris Wailes9385f9f2013-10-29 20:28:41 +00001065 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001066 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001067 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1068 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001069 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001070 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1071 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001072 return;
1073 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001074 } else {
1075 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1076 Attr.getName() << AANT_ArgumentIdentifier;
1077 return;
1078 }
1079
1080 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001081 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001082 Attr.getAttributeSpellingListIndex()));
1083}
1084
Chandler Carruthedc2c642011-07-02 00:01:44 +00001085static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1086 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001087 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001088 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001089}
1090
Chandler Carruthedc2c642011-07-02 00:01:44 +00001091static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001092 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001093 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1094 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001095 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001096 // Report warning about changed offset in the newer compiler versions.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001097 if (!FD->getType()->isDependentType() &&
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001098 !FD->getType()->isIncompleteType() && FD->isBitField() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001099 S.Context.getTypeAlign(FD->getType()) <= 8)
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001100 S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield);
1101
1102 FD->addAttr(::new (S.Context) PackedAttr(
1103 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001104 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001105 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001106}
1107
Ted Kremenek7fd17232011-09-29 07:02:25 +00001108static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1109 // The IBOutlet/IBOutletCollection attributes only apply to instance
1110 // variables or properties of Objective-C classes. The outlet must also
1111 // have an object reference type.
1112 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1113 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001114 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001115 << Attr.getName() << VD->getType() << 0;
1116 return false;
1117 }
1118 }
1119 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1120 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001121 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001122 << Attr.getName() << PD->getType() << 1;
1123 return false;
1124 }
1125 }
1126 else {
1127 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1128 return false;
1129 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001130
Ted Kremenek7fd17232011-09-29 07:02:25 +00001131 return true;
1132}
1133
Chandler Carruthedc2c642011-07-02 00:01:44 +00001134static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001135 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001136 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001137
Michael Han99315932013-01-24 16:46:58 +00001138 D->addAttr(::new (S.Context)
1139 IBOutletAttr(Attr.getRange(), S.Context,
1140 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001141}
1142
Chandler Carruthedc2c642011-07-02 00:01:44 +00001143static void handleIBOutletCollection(Sema &S, Decl *D,
1144 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001145
1146 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001147 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001148 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1149 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001150 return;
1151 }
1152
Ted Kremenek7fd17232011-09-29 07:02:25 +00001153 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001154 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001155
Richard Smithb1f9a282013-10-31 01:56:18 +00001156 ParsedType PT;
1157
1158 if (Attr.hasParsedType())
1159 PT = Attr.getTypeArg();
1160 else {
1161 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1162 S.getScopeForContext(D->getDeclContext()->getParent()));
1163 if (!PT) {
1164 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1165 return;
1166 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001167 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001168
Craig Topperc3ec1492014-05-26 06:22:03 +00001169 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001170 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1171 if (!QTLoc)
1172 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001173
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001174 // Diagnose use of non-object type in iboutletcollection attribute.
1175 // FIXME. Gnu attribute extension ignores use of builtin types in
1176 // attributes. So, __attribute__((iboutletcollection(char))) will be
1177 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001178 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001179 S.Diag(Attr.getLoc(),
1180 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1181 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001182 return;
1183 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001184
Michael Han99315932013-01-24 16:46:58 +00001185 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001186 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001187 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001188}
1189
Hal Finkelee90a222014-09-26 05:04:30 +00001190bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1191 if (RefOkay) {
1192 if (T->isReferenceType())
1193 return true;
1194 } else {
1195 T = T.getNonReferenceType();
1196 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001197
Hal Finkelee90a222014-09-26 05:04:30 +00001198 // The nonnull attribute, and other similar attributes, can be applied to a
1199 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001200 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001201 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1202 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001203 for (const auto *I : UD->fields()) {
1204 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001205 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1206 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001207 }
1208 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001209 }
1210
1211 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001212}
1213
Ted Kremenek9aedc152014-01-17 06:24:56 +00001214static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001215 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001216 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001217 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001218 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001219 if (isReturnValue)
1220 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1221 << Attr.getName() << AttrParmRange << TypeRange;
1222 else
1223 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1224 << Attr.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001225 return false;
1226 }
1227 return true;
1228}
1229
Chandler Carruthedc2c642011-07-02 00:01:44 +00001230static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001231 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001232 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1233 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001234 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001235 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001236 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001237
1238 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001239 if (Idx < getFunctionOrMethodNumParams(D) &&
1240 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001241 Ex->getSourceRange(),
1242 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001243 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001244
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001245 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001246 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001247
1248 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001249 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1250 // check if the attribute came from a macro expansion or a template
1251 // instantiation.
1252 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1253 S.ActiveTemplateInstantiations.empty()) {
1254 bool AnyPointers = isFunctionOrMethodVariadic(D);
1255 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1256 I != E && !AnyPointers; ++I) {
1257 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001258 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001259 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001261
Richard Smith588bd9b2014-08-27 04:59:42 +00001262 if (!AnyPointers)
1263 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001264 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001265
Richard Smith588bd9b2014-08-27 04:59:42 +00001266 unsigned *Start = NonNullArgs.data();
1267 unsigned Size = NonNullArgs.size();
1268 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001269 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001270 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001271 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001272}
1273
Jordan Rosec9399072014-02-11 17:27:59 +00001274static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1275 const AttributeList &Attr) {
1276 if (Attr.getNumArgs() > 0) {
1277 if (D->getFunctionType()) {
1278 handleNonNullAttr(S, D, Attr);
1279 } else {
1280 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1281 << D->getSourceRange();
1282 }
1283 return;
1284 }
1285
1286 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001287 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1288 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001289 return;
1290
1291 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001292 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001293 Attr.getAttributeSpellingListIndex()));
1294}
1295
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001296static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1297 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001298 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001299 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1300 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001301 /* isReturnValue */ true))
1302 return;
1303
1304 D->addAttr(::new (S.Context)
1305 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1306 Attr.getAttributeSpellingListIndex()));
1307}
1308
Hal Finkelee90a222014-09-26 05:04:30 +00001309static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1310 const AttributeList &Attr) {
1311 Expr *E = Attr.getArgAsExpr(0),
1312 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1313 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1314 Attr.getAttributeSpellingListIndex());
1315}
1316
1317void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1318 Expr *OE, unsigned SpellingListIndex) {
1319 QualType ResultType = getFunctionOrMethodResultType(D);
1320 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1321
1322 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1323 SourceLocation AttrLoc = AttrRange.getBegin();
1324
1325 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1326 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1327 << &TmpAttr << AttrRange << SR;
1328 return;
1329 }
1330
1331 if (!E->isValueDependent()) {
1332 llvm::APSInt I(64);
1333 if (!E->isIntegerConstantExpr(I, Context)) {
1334 if (OE)
1335 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1336 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1337 << E->getSourceRange();
1338 else
1339 Diag(AttrLoc, diag::err_attribute_argument_type)
1340 << &TmpAttr << AANT_ArgumentIntegerConstant
1341 << E->getSourceRange();
1342 return;
1343 }
1344
1345 if (!I.isPowerOf2()) {
1346 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1347 << E->getSourceRange();
1348 return;
1349 }
1350 }
1351
1352 if (OE) {
1353 if (!OE->isValueDependent()) {
1354 llvm::APSInt I(64);
1355 if (!OE->isIntegerConstantExpr(I, Context)) {
1356 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1357 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1358 << OE->getSourceRange();
1359 return;
1360 }
1361 }
1362 }
1363
1364 D->addAttr(::new (Context)
1365 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1366}
1367
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001368/// Normalize the attribute, __foo__ becomes foo.
1369/// Returns true if normalization was applied.
1370static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001371 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1372 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001373 AttrName = AttrName.drop_front(2).drop_back(2);
1374 return true;
1375 }
1376 return false;
1377}
1378
Chandler Carruthedc2c642011-07-02 00:01:44 +00001379static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001380 // This attribute must be applied to a function declaration. The first
1381 // argument to the attribute must be an identifier, the name of the resource,
1382 // for example: malloc. The following arguments must be argument indexes, the
1383 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001385 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001386 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001387
Aaron Ballman00e99962013-08-31 01:11:41 +00001388 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001389 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001390 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001391 return;
1392 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001393
Richard Smith852e9ce2013-11-27 01:46:48 +00001394 // Figure out our Kind.
1395 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001396 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001397 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001398
Richard Smith852e9ce2013-11-27 01:46:48 +00001399 // Check arguments.
1400 switch (K) {
1401 case OwnershipAttr::Takes:
1402 case OwnershipAttr::Holds:
1403 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001404 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1405 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001406 return;
1407 }
1408 break;
1409 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001410 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001411 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1412 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001413 return;
1414 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001415 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001416 }
1417
Richard Smith852e9ce2013-11-27 01:46:48 +00001418 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001419
Richard Smith852e9ce2013-11-27 01:46:48 +00001420 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001421 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001422 Module = &S.PP.getIdentifierTable().get(ModuleName);
1423 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001424
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001425 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001426 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1427 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001428 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001429 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001430 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001431
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001432 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001433 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001434 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001435 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001436 case OwnershipAttr::Takes:
1437 case OwnershipAttr::Holds:
1438 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1439 Err = 0;
1440 break;
1441 case OwnershipAttr::Returns:
1442 if (!T->isIntegerType())
1443 Err = 1;
1444 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001445 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001446 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001447 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001448 << Ex->getSourceRange();
1449 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001450 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001451
1452 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001453 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001454 // Cannot have two ownership attributes of different kinds for the same
1455 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001456 if (I->getOwnKind() != K && I->args_end() !=
1457 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001458 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001459 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001460 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001461 } else if (K == OwnershipAttr::Returns &&
1462 I->getOwnKind() == OwnershipAttr::Returns) {
1463 // A returns attribute conflicts with any other returns attribute using
1464 // a different index. Note, diagnostic reporting is 1-based, but stored
1465 // argument indexes are 0-based.
1466 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1467 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1468 << *(I->args_begin()) + 1;
1469 if (I->args_size())
1470 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1471 << (unsigned)Idx + 1 << Ex->getSourceRange();
1472 return;
1473 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001474 }
1475 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001476 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001477 }
1478
1479 unsigned* start = OwnershipArgs.data();
1480 unsigned size = OwnershipArgs.size();
1481 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001482
Michael Han99315932013-01-24 16:46:58 +00001483 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001484 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001485 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001486}
1487
Chandler Carruthedc2c642011-07-02 00:01:44 +00001488static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001489 // Check the attribute arguments.
1490 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1492 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001493 return;
1494 }
1495
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001496 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001497
Rafael Espindolac18086a2010-02-23 22:00:30 +00001498 // gcc rejects
1499 // class c {
1500 // static int a __attribute__((weakref ("v2")));
1501 // static int b() __attribute__((weakref ("f3")));
1502 // };
1503 // and ignores the attributes of
1504 // void f(void) {
1505 // static int a __attribute__((weakref ("v2")));
1506 // }
1507 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001508 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001509 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001510 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1511 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001512 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001513 }
1514
1515 // The GCC manual says
1516 //
1517 // At present, a declaration to which `weakref' is attached can only
1518 // be `static'.
1519 //
1520 // It also says
1521 //
1522 // Without a TARGET,
1523 // given as an argument to `weakref' or to `alias', `weakref' is
1524 // equivalent to `weak'.
1525 //
1526 // gcc 4.4.1 will accept
1527 // int a7 __attribute__((weakref));
1528 // as
1529 // int a7 __attribute__((weak));
1530 // This looks like a bug in gcc. We reject that for now. We should revisit
1531 // it if this behaviour is actually used.
1532
Rafael Espindolac18086a2010-02-23 22:00:30 +00001533 // GCC rejects
1534 // static ((alias ("y"), weakref)).
1535 // Should we? How to check that weakref is before or after alias?
1536
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001537 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1538 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1539 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001540 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001541 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001542 // GCC will accept anything as the argument of weakref. Should we
1543 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001544 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1545 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001546
Michael Han99315932013-01-24 16:46:58 +00001547 D->addAttr(::new (S.Context)
1548 WeakRefAttr(Attr.getRange(), S.Context,
1549 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001550}
1551
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001552static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1553 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001554 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001555 return;
1556
Douglas Gregore8bbc122011-09-02 00:18:52 +00001557 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001558 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1559 return;
1560 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001561 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
1562 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_nvptx);
1563 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001564
David Majnemer2dc81462015-01-19 09:00:28 +00001565 // Aliases should be on declarations, not definitions.
1566 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1567 if (FD->isThisDeclarationADefinition()) {
1568 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1569 return;
1570 }
1571 } else {
1572 const auto *VD = cast<VarDecl>(D);
1573 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1574 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1575 return;
1576 }
1577 }
1578
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001579 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001580
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001581 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001582 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001583}
1584
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001585static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001586 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001587 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001588
Michael Han99315932013-01-24 16:46:58 +00001589 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1590 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001591}
1592
1593static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001594 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001595 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001596
Michael Han99315932013-01-24 16:46:58 +00001597 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1598 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001599}
1600
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001601static void handleTLSModelAttr(Sema &S, Decl *D,
1602 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001603 StringRef Model;
1604 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001605 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001606 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001607 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001608
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001609 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001610 if (Model != "global-dynamic" && Model != "local-dynamic"
1611 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001612 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001613 return;
1614 }
1615
Michael Han99315932013-01-24 16:46:58 +00001616 D->addAttr(::new (S.Context)
1617 TLSModelAttr(Attr.getRange(), S.Context, Model,
1618 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001619}
1620
David Majnemer631a90b2015-02-04 07:23:21 +00001621static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1622 QualType ResultType = getFunctionOrMethodResultType(D);
1623 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1624 D->addAttr(::new (S.Context) RestrictAttr(
1625 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1626 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001627 }
1628
David Majnemer631a90b2015-02-04 07:23:21 +00001629 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1630 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001631}
1632
Chandler Carruthedc2c642011-07-02 00:01:44 +00001633static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001634 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001635 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001636 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001637 return;
1638 }
1639
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001640 if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(),
1641 Attr.getAttributeSpellingListIndex()))
1642 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001643}
1644
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001645static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1646 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(),
1647 Attr.getName()))
1648 return;
1649
1650 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context,
1651 Attr.getAttributeSpellingListIndex()));
1652}
1653
Chandler Carruthedc2c642011-07-02 00:01:44 +00001654static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001655 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001656
1657 if (S.CheckNoReturnAttr(attr)) return;
1658
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001659 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001660 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001661 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001662 return;
1663 }
1664
Michael Han99315932013-01-24 16:46:58 +00001665 D->addAttr(::new (S.Context)
1666 NoReturnAttr(attr.getRange(), S.Context,
1667 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001668}
1669
1670bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001671 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001672 attr.setInvalid();
1673 return true;
1674 }
1675
1676 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001677}
1678
Chandler Carruthedc2c642011-07-02 00:01:44 +00001679static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1680 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001681
1682 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1683 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001684 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001685 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001686 if (!VD || (!VD->getType()->isBlockPointerType() &&
1687 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001688 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001689 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001690 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001691 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001692 return;
1693 }
1694 }
1695
Michael Han99315932013-01-24 16:46:58 +00001696 D->addAttr(::new (S.Context)
1697 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1698 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001699}
1700
John Thompsoncdb847ba2010-08-09 21:53:52 +00001701// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001702static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001703/*
1704 Returning a Vector Class in Registers
1705
Eric Christopherbc638a82010-12-01 22:13:54 +00001706 According to the PPU ABI specifications, a class with a single member of
1707 vector type is returned in memory when used as the return value of a function.
1708 This results in inefficient code when implementing vector classes. To return
1709 the value in a single vector register, add the vecreturn attribute to the
1710 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001711
1712 Example:
1713
1714 struct Vector
1715 {
1716 __vector float xyzw;
1717 } __attribute__((vecreturn));
1718
1719 Vector Add(Vector lhs, Vector rhs)
1720 {
1721 Vector result;
1722 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1723 return result; // This will be returned in a register
1724 }
1725*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001726 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1727 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001728 return;
1729 }
1730
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001731 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001732 int count = 0;
1733
1734 if (!isa<CXXRecordDecl>(record)) {
1735 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1736 return;
1737 }
1738
1739 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1740 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1741 return;
1742 }
1743
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001744 for (const auto *I : record->fields()) {
1745 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001746 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1747 return;
1748 }
1749 count++;
1750 }
1751
Michael Han99315932013-01-24 16:46:58 +00001752 D->addAttr(::new (S.Context)
1753 VecReturnAttr(Attr.getRange(), S.Context,
1754 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001755}
1756
Richard Smithe233fbf2013-01-28 22:42:45 +00001757static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1758 const AttributeList &Attr) {
1759 if (isa<ParmVarDecl>(D)) {
1760 // [[carries_dependency]] can only be applied to a parameter if it is a
1761 // parameter of a function declaration or lambda.
1762 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1763 S.Diag(Attr.getLoc(),
1764 diag::err_carries_dependency_param_not_function_decl);
1765 return;
1766 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001767 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001768
1769 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1770 Attr.getRange(), S.Context,
1771 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001772}
1773
Akira Hatanakac8667622015-11-06 23:56:15 +00001774static void handleNotTailCalledAttr(Sema &S, Decl *D,
1775 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001776 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(),
1777 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00001778 return;
1779
1780 D->addAttr(::new (S.Context) NotTailCalledAttr(
1781 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1782}
1783
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001784static void handleDisableTailCallsAttr(Sema &S, Decl *D,
1785 const AttributeList &Attr) {
1786 if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(),
1787 Attr.getName()))
1788 return;
1789
1790 D->addAttr(::new (S.Context) DisableTailCallsAttr(
1791 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1792}
1793
Chandler Carruthedc2c642011-07-02 00:01:44 +00001794static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001795 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001796 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001797 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001798 return;
1799 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001800 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001801 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001802 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001803 return;
1804 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001805
Michael Han99315932013-01-24 16:46:58 +00001806 D->addAttr(::new (S.Context)
1807 UsedAttr(Attr.getRange(), S.Context,
1808 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001809}
1810
Chandler Carruthedc2c642011-07-02 00:01:44 +00001811static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001812 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001813 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001814 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1815 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001816
Michael Han99315932013-01-24 16:46:58 +00001817 D->addAttr(::new (S.Context)
1818 ConstructorAttr(Attr.getRange(), S.Context, priority,
1819 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001820}
1821
Chandler Carruthedc2c642011-07-02 00:01:44 +00001822static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001823 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001824 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001825 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1826 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001827
Michael Han99315932013-01-24 16:46:58 +00001828 D->addAttr(::new (S.Context)
1829 DestructorAttr(Attr.getRange(), S.Context, priority,
1830 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001831}
1832
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001833template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001834static void handleAttrWithMessage(Sema &S, Decl *D,
1835 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001836 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001837 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001838 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001839 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001840
Michael Han99315932013-01-24 16:46:58 +00001841 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1842 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001843}
1844
Ted Kremenek438f8db2014-02-22 01:06:05 +00001845static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001846 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001847 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001848 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1849 << Attr.getName() << Attr.getRange();
1850 return;
1851 }
1852
Ted Kremenek28eace62013-11-23 01:01:34 +00001853 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001854 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1855 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001856}
1857
Jordy Rose740b0c22012-05-08 03:27:22 +00001858static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1859 IdentifierInfo *Platform,
1860 VersionTuple Introduced,
1861 VersionTuple Deprecated,
1862 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001863 StringRef PlatformName
1864 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1865 if (PlatformName.empty())
1866 PlatformName = Platform->getName();
1867
1868 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1869 // of these steps are needed).
1870 if (!Introduced.empty() && !Deprecated.empty() &&
1871 !(Introduced <= Deprecated)) {
1872 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1873 << 1 << PlatformName << Deprecated.getAsString()
1874 << 0 << Introduced.getAsString();
1875 return true;
1876 }
1877
1878 if (!Introduced.empty() && !Obsoleted.empty() &&
1879 !(Introduced <= Obsoleted)) {
1880 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1881 << 2 << PlatformName << Obsoleted.getAsString()
1882 << 0 << Introduced.getAsString();
1883 return true;
1884 }
1885
1886 if (!Deprecated.empty() && !Obsoleted.empty() &&
1887 !(Deprecated <= Obsoleted)) {
1888 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1889 << 2 << PlatformName << Obsoleted.getAsString()
1890 << 1 << Deprecated.getAsString();
1891 return true;
1892 }
1893
1894 return false;
1895}
1896
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001897/// \brief Check whether the two versions match.
1898///
1899/// If either version tuple is empty, then they are assumed to match. If
1900/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1901static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1902 bool BeforeIsOkay) {
1903 if (X.empty() || Y.empty())
1904 return true;
1905
1906 if (X == Y)
1907 return true;
1908
1909 if (BeforeIsOkay && X < Y)
1910 return true;
1911
1912 return false;
1913}
1914
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001915AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001916 IdentifierInfo *Platform,
1917 VersionTuple Introduced,
1918 VersionTuple Deprecated,
1919 VersionTuple Obsoleted,
1920 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001921 StringRef Message,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001922 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001923 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001924 VersionTuple MergedIntroduced = Introduced;
1925 VersionTuple MergedDeprecated = Deprecated;
1926 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001927 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001928 bool OverrideOrImpl = false;
1929 switch (AMK) {
1930 case AMK_None:
1931 case AMK_Redeclaration:
1932 OverrideOrImpl = false;
1933 break;
1934
1935 case AMK_Override:
1936 case AMK_ProtocolImplementation:
1937 OverrideOrImpl = true;
1938 break;
1939 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001940
Rafael Espindolac67f2232012-05-10 02:50:16 +00001941 if (D->hasAttrs()) {
1942 AttrVec &Attrs = D->getAttrs();
1943 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1944 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1945 if (!OldAA) {
1946 ++i;
1947 continue;
1948 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001949
Rafael Espindolac67f2232012-05-10 02:50:16 +00001950 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1951 if (OldPlatform != Platform) {
1952 ++i;
1953 continue;
1954 }
1955
Tim Northover7a73cc72015-10-30 16:30:49 +00001956 // If there is an existing availability attribute for this platform that
1957 // is explicit and the new one is implicit use the explicit one and
1958 // discard the new implicit attribute.
1959 if (OldAA->getRange().isValid() && Range.isInvalid()) {
1960 return nullptr;
1961 }
1962
1963 // If there is an existing attribute for this platform that is implicit
1964 // and the new attribute is explicit then erase the old one and
1965 // continue processing the attributes.
1966 if (Range.isValid() && OldAA->getRange().isInvalid()) {
1967 Attrs.erase(Attrs.begin() + i);
1968 --e;
1969 continue;
1970 }
1971
Rafael Espindolac67f2232012-05-10 02:50:16 +00001972 FoundAny = true;
1973 VersionTuple OldIntroduced = OldAA->getIntroduced();
1974 VersionTuple OldDeprecated = OldAA->getDeprecated();
1975 VersionTuple OldObsoleted = OldAA->getObsoleted();
1976 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001977
Douglas Gregord2a713e2015-09-30 21:27:42 +00001978 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
1979 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
1980 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001981 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00001982 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
1983 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001984 int Which = -1;
1985 VersionTuple FirstVersion;
1986 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001987 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001988 Which = 0;
1989 FirstVersion = OldIntroduced;
1990 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001991 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001992 Which = 1;
1993 FirstVersion = Deprecated;
1994 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001995 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001996 Which = 2;
1997 FirstVersion = Obsoleted;
1998 SecondVersion = OldObsoleted;
1999 }
2000
2001 if (Which == -1) {
2002 Diag(OldAA->getLocation(),
2003 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002004 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2005 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002006 } else {
2007 Diag(OldAA->getLocation(),
2008 diag::warn_mismatched_availability_override)
2009 << Which
2010 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002011 << FirstVersion.getAsString() << SecondVersion.getAsString()
2012 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002013 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002014 if (AMK == AMK_Override)
2015 Diag(Range.getBegin(), diag::note_overridden_method);
2016 else
2017 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002018 } else {
2019 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2020 Diag(Range.getBegin(), diag::note_previous_attribute);
2021 }
2022
Rafael Espindolac67f2232012-05-10 02:50:16 +00002023 Attrs.erase(Attrs.begin() + i);
2024 --e;
2025 continue;
2026 }
2027
2028 VersionTuple MergedIntroduced2 = MergedIntroduced;
2029 VersionTuple MergedDeprecated2 = MergedDeprecated;
2030 VersionTuple MergedObsoleted2 = MergedObsoleted;
2031
2032 if (MergedIntroduced2.empty())
2033 MergedIntroduced2 = OldIntroduced;
2034 if (MergedDeprecated2.empty())
2035 MergedDeprecated2 = OldDeprecated;
2036 if (MergedObsoleted2.empty())
2037 MergedObsoleted2 = OldObsoleted;
2038
2039 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2040 MergedIntroduced2, MergedDeprecated2,
2041 MergedObsoleted2)) {
2042 Attrs.erase(Attrs.begin() + i);
2043 --e;
2044 continue;
2045 }
2046
2047 MergedIntroduced = MergedIntroduced2;
2048 MergedDeprecated = MergedDeprecated2;
2049 MergedObsoleted = MergedObsoleted2;
2050 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002051 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002052 }
2053
2054 if (FoundAny &&
2055 MergedIntroduced == Introduced &&
2056 MergedDeprecated == Deprecated &&
2057 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002058 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002059
Douglas Gregord2a713e2015-09-30 21:27:42 +00002060 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002061 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002062 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002063 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002064 !OverrideOrImpl) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002065 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2066 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002067 Obsoleted, IsUnavailable, Message,
2068 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002069 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002070 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002071}
2072
Chandler Carruthedc2c642011-07-02 00:01:44 +00002073static void handleAvailabilityAttr(Sema &S, Decl *D,
2074 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002075 if (!checkAttributeNumArgs(S, Attr, 1))
2076 return;
2077 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002078 unsigned Index = Attr.getAttributeSpellingListIndex();
2079
Aaron Ballman00e99962013-08-31 01:11:41 +00002080 IdentifierInfo *II = Platform->Ident;
2081 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2082 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2083 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002084
Rafael Espindolac231fab2013-01-08 21:30:32 +00002085 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2086 if (!ND) {
2087 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2088 return;
2089 }
2090
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002091 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2092 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2093 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002094 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002095 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002096 if (const StringLiteral *SE =
2097 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002098 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002099
Aaron Ballman00e99962013-08-31 01:11:41 +00002100 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002101 Introduced.Version,
2102 Deprecated.Version,
2103 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002104 IsUnavailable, Str,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002105 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002106 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002107 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002108 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002109
2110 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2111 // matches before the start of the watchOS platform.
2112 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2113 IdentifierInfo *NewII = nullptr;
2114 if (II->getName() == "ios")
2115 NewII = &S.Context.Idents.get("watchos");
2116 else if (II->getName() == "ios_app_extension")
2117 NewII = &S.Context.Idents.get("watchos_app_extension");
2118
2119 if (NewII) {
2120 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2121 if (Version.empty())
2122 return Version;
2123 auto Major = Version.getMajor();
2124 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2125 if (NewMajor >= 2) {
2126 if (Version.getMinor().hasValue()) {
2127 if (Version.getSubminor().hasValue())
2128 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2129 Version.getSubminor().getValue());
2130 else
2131 return VersionTuple(NewMajor, Version.getMinor().getValue());
2132 }
2133 }
2134
2135 return VersionTuple(2, 0);
2136 };
2137
2138 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2139 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2140 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2141
2142 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2143 SourceRange(),
2144 NewII,
2145 NewIntroduced,
2146 NewDeprecated,
2147 NewObsoleted,
2148 IsUnavailable, Str,
2149 Sema::AMK_None,
2150 Index);
2151 if (NewAttr)
2152 D->addAttr(NewAttr);
2153 }
2154 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2155 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2156 // matches before the start of the tvOS platform.
2157 IdentifierInfo *NewII = nullptr;
2158 if (II->getName() == "ios")
2159 NewII = &S.Context.Idents.get("tvos");
2160 else if (II->getName() == "ios_app_extension")
2161 NewII = &S.Context.Idents.get("tvos_app_extension");
2162
2163 if (NewII) {
2164 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2165 SourceRange(),
2166 NewII,
2167 Introduced.Version,
2168 Deprecated.Version,
2169 Obsoleted.Version,
2170 IsUnavailable, Str,
2171 Sema::AMK_None,
2172 Index);
2173 if (NewAttr)
2174 D->addAttr(NewAttr);
2175 }
2176 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002177}
2178
John McCalld041a9b2013-02-20 01:54:26 +00002179template <class T>
2180static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2181 typename T::VisibilityType value,
2182 unsigned attrSpellingListIndex) {
2183 T *existingAttr = D->getAttr<T>();
2184 if (existingAttr) {
2185 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2186 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002187 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002188 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2189 S.Diag(range.getBegin(), diag::note_previous_attribute);
2190 D->dropAttr<T>();
2191 }
2192 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2193}
2194
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002195VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002196 VisibilityAttr::VisibilityType Vis,
2197 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002198 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2199 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002200}
2201
John McCalld041a9b2013-02-20 01:54:26 +00002202TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2203 TypeVisibilityAttr::VisibilityType Vis,
2204 unsigned AttrSpellingListIndex) {
2205 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2206 AttrSpellingListIndex);
2207}
2208
2209static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2210 bool isTypeVisibility) {
2211 // Visibility attributes don't mean anything on a typedef.
2212 if (isa<TypedefNameDecl>(D)) {
2213 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2214 << Attr.getName();
2215 return;
2216 }
2217
2218 // 'type_visibility' can only go on a type or namespace.
2219 if (isTypeVisibility &&
2220 !(isa<TagDecl>(D) ||
2221 isa<ObjCInterfaceDecl>(D) ||
2222 isa<NamespaceDecl>(D))) {
2223 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2224 << Attr.getName() << ExpectedTypeOrNamespace;
2225 return;
2226 }
2227
Benjamin Kramer70370212013-09-09 15:08:57 +00002228 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002229 StringRef TypeStr;
2230 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002231 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002232 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002233
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002234 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002235 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002236 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002237 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002238 return;
2239 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002240
2241 // Complain about attempts to use protected visibility on targets
2242 // (like Darwin) that don't support it.
2243 if (type == VisibilityAttr::Protected &&
2244 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2245 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2246 type = VisibilityAttr::Default;
2247 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002248
Michael Han99315932013-01-24 16:46:58 +00002249 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002250 clang::Attr *newAttr;
2251 if (isTypeVisibility) {
2252 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2253 (TypeVisibilityAttr::VisibilityType) type,
2254 Index);
2255 } else {
2256 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2257 }
2258 if (newAttr)
2259 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002260}
2261
Chandler Carruthedc2c642011-07-02 00:01:44 +00002262static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2263 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002264 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002265 if (!Attr.isArgIdent(0)) {
2266 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2267 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002268 return;
2269 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002270
Aaron Ballman682ee422013-09-11 19:47:58 +00002271 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2272 ObjCMethodFamilyAttr::FamilyKind F;
2273 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2274 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2275 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002276 return;
2277 }
2278
Alp Toker314cc812014-01-25 16:55:45 +00002279 if (F == ObjCMethodFamilyAttr::OMF_init &&
2280 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002281 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002282 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002283 // Ignore the attribute.
2284 return;
2285 }
2286
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002287 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002288 S.Context, F,
2289 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002290}
2291
Chandler Carruthedc2c642011-07-02 00:01:44 +00002292static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002293 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002294 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002295 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002296 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2297 return;
2298 }
2299 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002300 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2301 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002302 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002303 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2304 return;
2305 }
2306 }
2307 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002308 // It is okay to include this attribute on properties, e.g.:
2309 //
2310 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2311 //
2312 // In this case it follows tradition and suppresses an error in the above
2313 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002314 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002315 }
Michael Han99315932013-01-24 16:46:58 +00002316 D->addAttr(::new (S.Context)
2317 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2318 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002319}
2320
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002321static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2322 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2323 QualType T = TD->getUnderlyingType();
2324 if (!T->isObjCObjectPointerType()) {
2325 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2326 return;
2327 }
2328 } else {
2329 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2330 return;
2331 }
2332 D->addAttr(::new (S.Context)
2333 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2334 Attr.getAttributeSpellingListIndex()));
2335}
2336
Chandler Carruthedc2c642011-07-02 00:01:44 +00002337static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002338 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002339 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002340 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002341 return;
2342 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002343
Aaron Ballman00e99962013-08-31 01:11:41 +00002344 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002345 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002346 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2347 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2348 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002349 return;
2350 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002351
Michael Han99315932013-01-24 16:46:58 +00002352 D->addAttr(::new (S.Context)
2353 BlocksAttr(Attr.getRange(), S.Context, type,
2354 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002355}
2356
Chandler Carruthedc2c642011-07-02 00:01:44 +00002357static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002358 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002359 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002360 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002361 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002362 if (E->isTypeDependent() || E->isValueDependent() ||
2363 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002364 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002365 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002366 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002367 return;
2368 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002369
John McCallb46f2872011-09-09 07:56:05 +00002370 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002371 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2372 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002373 return;
2374 }
John McCallb46f2872011-09-09 07:56:05 +00002375
2376 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002377 }
2378
Aaron Ballman18a78382013-11-21 00:28:23 +00002379 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002380 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002381 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002382 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002383 if (E->isTypeDependent() || E->isValueDependent() ||
2384 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002385 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002386 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002387 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002388 return;
2389 }
2390 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002391
John McCallb46f2872011-09-09 07:56:05 +00002392 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002393 // FIXME: This error message could be improved, it would be nice
2394 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002395 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2396 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002397 return;
2398 }
2399 }
2400
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002401 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002402 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002403 if (isa<FunctionNoProtoType>(FT)) {
2404 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2405 return;
2406 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002407
Chris Lattner9363e312009-03-17 23:03:47 +00002408 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002409 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002410 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002411 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002412 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002413 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002414 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002415 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002416 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002417 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2418 if (!BD->isVariadic()) {
2419 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2420 return;
2421 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002422 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002423 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002424 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002425 const FunctionType *FT = Ty->isFunctionPointerType()
2426 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002427 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002428 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002429 int m = Ty->isFunctionPointerType() ? 0 : 1;
2430 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002431 return;
2432 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002433 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002434 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002435 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002436 return;
2437 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002438 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002439 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002440 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002441 return;
2442 }
Michael Han99315932013-01-24 16:46:58 +00002443 D->addAttr(::new (S.Context)
2444 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2445 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002446}
2447
Chandler Carruthedc2c642011-07-02 00:01:44 +00002448static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002449 if (D->getFunctionType() &&
2450 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002451 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2452 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002453 return;
2454 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002455 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002456 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002457 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2458 << Attr.getName() << 1;
2459 return;
2460 }
2461
Michael Han99315932013-01-24 16:46:58 +00002462 D->addAttr(::new (S.Context)
2463 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2464 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002465}
2466
Chandler Carruthedc2c642011-07-02 00:01:44 +00002467static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002468 // weak_import only applies to variable & function declarations.
2469 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002470 if (!D->canBeWeakImported(isDef)) {
2471 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002472 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2473 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002474 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002475 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002476 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002477 // Nothing to warn about here.
2478 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002479 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002480 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002481
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002482 return;
2483 }
2484
Michael Han99315932013-01-24 16:46:58 +00002485 D->addAttr(::new (S.Context)
2486 WeakImportAttr(Attr.getRange(), S.Context,
2487 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002488}
2489
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002490// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002491template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002492static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002493 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002494 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002495 for (unsigned i = 0; i < 3; ++i) {
2496 const Expr *E = Attr.getArgAsExpr(i);
2497 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002498 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002499 if (WGSize[i] == 0) {
2500 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2501 << Attr.getName() << E->getSourceRange();
2502 return;
2503 }
2504 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002505
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002506 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2507 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2508 Existing->getYDim() == WGSize[1] &&
2509 Existing->getZDim() == WGSize[2]))
2510 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002511
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002512 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2513 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002514 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002515}
2516
Joey Goulyaba589c2013-03-08 09:42:32 +00002517static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002518 if (!Attr.hasParsedType()) {
2519 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2520 << Attr.getName() << 1;
2521 return;
2522 }
2523
Craig Topperc3ec1492014-05-26 06:22:03 +00002524 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002525 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2526 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002527
2528 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2529 (ParmType->isBooleanType() ||
2530 !ParmType->isIntegralType(S.getASTContext()))) {
2531 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2532 << ParmType;
2533 return;
2534 }
2535
Aaron Ballmana9e05402013-12-02 22:16:55 +00002536 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002537 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002538 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2539 return;
2540 }
2541 }
2542
2543 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002544 ParmTSI,
2545 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002546}
2547
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002548SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002549 StringRef Name,
2550 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002551 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2552 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002553 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002554 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2555 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002556 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002557 }
Michael Han99315932013-01-24 16:46:58 +00002558 return ::new (Context) SectionAttr(Range, Context, Name,
2559 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002560}
2561
Reid Kleckner2a133222015-03-04 23:39:17 +00002562bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2563 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2564 if (!Error.empty()) {
2565 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2566 return false;
2567 }
2568 return true;
2569}
2570
Chandler Carruthedc2c642011-07-02 00:01:44 +00002571static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002572 // Make sure that there is a string literal as the sections's single
2573 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002574 StringRef Str;
2575 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002576 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002577 return;
Mike Stump11289f42009-09-09 15:08:12 +00002578
Reid Kleckner2a133222015-03-04 23:39:17 +00002579 if (!S.checkSectionName(LiteralLoc, Str))
2580 return;
2581
Chris Lattner30ba6742009-08-10 19:03:04 +00002582 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002583 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002584 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002585 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002586 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002587 return;
2588 }
Mike Stump11289f42009-09-09 15:08:12 +00002589
Michael Han99315932013-01-24 16:46:58 +00002590 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002591 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002592 if (NewAttr)
2593 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002594}
2595
Eric Christopher789a7ad2015-06-12 01:36:05 +00002596// Check for things we'd like to warn about, no errors or validation for now.
2597// TODO: Validation should use a backend target library that specifies
2598// the allowable subtarget features and cpus. We could use something like a
2599// TargetCodeGenInfo hook here to do validation.
2600void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2601 for (auto Str : {"tune=", "fpmath="})
2602 if (AttrStr.find(Str) != StringRef::npos)
2603 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2604}
2605
Eric Christopher11acf732015-06-12 01:35:52 +00002606static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002607 StringRef Str;
2608 SourceLocation LiteralLoc;
2609 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2610 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002611 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002612 unsigned Index = Attr.getAttributeSpellingListIndex();
2613 TargetAttr *NewAttr =
2614 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2615 D->addAttr(NewAttr);
2616}
2617
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002618
Chandler Carruthedc2c642011-07-02 00:01:44 +00002619static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002620 VarDecl *VD = cast<VarDecl>(D);
2621 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002622 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002623 return;
2624 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002625
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002626 Expr *E = Attr.getArgAsExpr(0);
2627 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002628 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002629 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002630
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002631 // gcc only allows for simple identifiers. Since we support more than gcc, we
2632 // will warn the user.
2633 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2634 if (DRE->hasQualifier())
2635 S.Diag(Loc, diag::warn_cleanup_ext);
2636 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2637 NI = DRE->getNameInfo();
2638 if (!FD) {
2639 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2640 << NI.getName();
2641 return;
2642 }
2643 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2644 if (ULE->hasExplicitTemplateArgs())
2645 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002646 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2647 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002648 if (!FD) {
2649 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2650 << NI.getName();
2651 if (ULE->getType() == S.Context.OverloadTy)
2652 S.NoteAllOverloadCandidates(ULE);
2653 return;
2654 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002655 } else {
2656 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002657 return;
2658 }
2659
Anders Carlssond277d792009-01-31 01:16:18 +00002660 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002661 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2662 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002663 return;
2664 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002665
Anders Carlsson723f55d2009-02-07 23:16:50 +00002666 // We're currently more strict than GCC about what function types we accept.
2667 // If this ever proves to be a problem it should be easy to fix.
2668 QualType Ty = S.Context.getPointerType(VD->getType());
2669 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002670 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2671 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002672 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2673 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002674 return;
2675 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002676
Michael Han99315932013-01-24 16:46:58 +00002677 D->addAttr(::new (S.Context)
2678 CleanupAttr(Attr.getRange(), S.Context, FD,
2679 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002680}
2681
Mike Stumpd3bb5572009-07-24 19:02:52 +00002682/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002683/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002684static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002685 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002686 uint64_t Idx;
2687 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002688 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002689
Eric Christopherb64963e2015-08-13 21:34:35 +00002690 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002691 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002692
Eric Christopherb64963e2015-08-13 21:34:35 +00002693 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2694 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002695 !isCFStringType(Ty, S.Context) &&
2696 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002697 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002698 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002699 << "a string type" << IdxExpr->getSourceRange()
2700 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002701 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002702 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002703 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002704 if (!isNSStringType(Ty, S.Context) &&
2705 !isCFStringType(Ty, S.Context) &&
2706 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002707 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002708 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002709 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002710 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002711 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002712 }
2713
Alp Toker601b22c2014-01-21 23:35:24 +00002714 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002715 // because that has corrected for the implicit this parameter, and is zero-
2716 // based. The attribute expects what the user wrote explicitly.
2717 llvm::APSInt Val;
2718 IdxExpr->EvaluateAsInt(Val, S.Context);
2719
Michael Han99315932013-01-24 16:46:58 +00002720 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002721 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002722 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002723}
2724
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002725enum FormatAttrKind {
2726 CFStringFormat,
2727 NSStringFormat,
2728 StrftimeFormat,
2729 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002730 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002731 InvalidFormat
2732};
2733
2734/// getFormatAttrKind - Map from format attribute names to supported format
2735/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002736static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002737 return llvm::StringSwitch<FormatAttrKind>(Format)
2738 // Check for formats that get handled specially.
2739 .Case("NSString", NSStringFormat)
2740 .Case("CFString", CFStringFormat)
2741 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002742
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002743 // Otherwise, check for supported formats.
2744 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2745 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2746 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002747 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002748 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002749
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002750 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2751 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002752}
2753
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002754/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002755/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002756static void handleInitPriorityAttr(Sema &S, Decl *D,
2757 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002758 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002759 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2760 return;
2761 }
2762
Aaron Ballman4a611152013-11-27 16:34:09 +00002763 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002764 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2765 Attr.setInvalid();
2766 return;
2767 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002768 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002769 if (S.Context.getAsArrayType(T))
2770 T = S.Context.getBaseElementType(T);
2771 if (!T->getAs<RecordType>()) {
2772 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2773 Attr.setInvalid();
2774 return;
2775 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002776
2777 Expr *E = Attr.getArgAsExpr(0);
2778 uint32_t prioritynum;
2779 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002780 Attr.setInvalid();
2781 return;
2782 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002783
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002784 if (prioritynum < 101 || prioritynum > 65535) {
2785 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00002786 << E->getSourceRange() << Attr.getName() << 101 << 65535;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002787 Attr.setInvalid();
2788 return;
2789 }
Michael Han99315932013-01-24 16:46:58 +00002790 D->addAttr(::new (S.Context)
2791 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2792 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002793}
2794
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002795FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2796 IdentifierInfo *Format, int FormatIdx,
2797 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002798 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002799 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002800 for (auto *F : D->specific_attrs<FormatAttr>()) {
2801 if (F->getType() == Format &&
2802 F->getFormatIdx() == FormatIdx &&
2803 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002804 // If we don't have a valid location for this attribute, adopt the
2805 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002806 if (F->getLocation().isInvalid())
2807 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002808 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002809 }
2810 }
2811
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002812 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2813 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002814}
2815
Mike Stumpd3bb5572009-07-24 19:02:52 +00002816/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002817/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002818static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002819 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002820 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002821 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002822 return;
2823 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002824
Chandler Carruth743682b2010-11-16 08:35:43 +00002825 // In C++ the implicit 'this' function parameter also counts, and they are
2826 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002827 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002828 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002829
Aaron Ballman00e99962013-08-31 01:11:41 +00002830 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2831 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002832
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002833 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002834 // If we've modified the string name, we need a new identifier for it.
2835 II = &S.Context.Idents.get(Format);
2836 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002837
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002838 // Check for supported formats.
2839 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002840
2841 if (Kind == IgnoredFormat)
2842 return;
2843
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002844 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002845 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002846 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002847 return;
2848 }
2849
2850 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002851 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002852 uint32_t Idx;
2853 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002854 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002855
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002856 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002857 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002858 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002859 return;
2860 }
2861
2862 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002863 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002864
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002865 if (HasImplicitThisParam) {
2866 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002867 S.Diag(Attr.getLoc(),
2868 diag::err_format_attribute_implicit_this_format_string)
2869 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002870 return;
2871 }
2872 ArgIdx--;
2873 }
Mike Stump11289f42009-09-09 15:08:12 +00002874
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002875 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002876 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002877
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002878 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002879 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002880 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002881 << "a CFString" << IdxExpr->getSourceRange()
2882 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002883 return;
2884 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002885 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002886 // FIXME: do we need to check if the type is NSString*? What are the
2887 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002888 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002889 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002890 << "an NSString" << IdxExpr->getSourceRange()
2891 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002892 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002893 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002894 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002895 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002896 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002897 << "a string type" << IdxExpr->getSourceRange()
2898 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002899 return;
2900 }
2901
2902 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002903 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002904 uint32_t FirstArg;
2905 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002906 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002907
2908 // check if the function is variadic if the 3rd argument non-zero
2909 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002910 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002911 ++NumArgs; // +1 for ...
2912 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002913 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002914 return;
2915 }
2916 }
2917
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002918 // strftime requires FirstArg to be 0 because it doesn't read from any
2919 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002920 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002921 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002922 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2923 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002924 return;
2925 }
2926 // if 0 it disables parameter checking (to use with e.g. va_list)
2927 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002928 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002929 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002930 return;
2931 }
2932
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002933 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002934 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002935 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002936 if (NewAttr)
2937 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002938}
2939
Chandler Carruthedc2c642011-07-02 00:01:44 +00002940static void handleTransparentUnionAttr(Sema &S, Decl *D,
2941 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002942 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002943 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002944 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002945 if (TD && TD->getUnderlyingType()->isUnionType())
2946 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2947 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002948 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002949
2950 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002951 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002952 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002953 return;
2954 }
2955
John McCallf937c022011-10-07 06:10:15 +00002956 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002957 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002958 diag::warn_transparent_union_attribute_not_definition);
2959 return;
2960 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002961
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002962 RecordDecl::field_iterator Field = RD->field_begin(),
2963 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002964 if (Field == FieldEnd) {
2965 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2966 return;
2967 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002968
David Blaikie40ed2972012-06-06 20:45:41 +00002969 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002970 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002971 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002972 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002973 diag::warn_transparent_union_attribute_floating)
2974 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002975 return;
2976 }
2977
2978 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2979 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2980 for (; Field != FieldEnd; ++Field) {
2981 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002982 // FIXME: this isn't fully correct; we also need to test whether the
2983 // members of the union would all have the same calling convention as the
2984 // first member of the union. Checking just the size and alignment isn't
2985 // sufficient (consider structs passed on the stack instead of in registers
2986 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002987 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002988 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002989 // Warn if we drop the attribute.
2990 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002991 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002992 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002993 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002994 diag::warn_transparent_union_attribute_field_size_align)
2995 << isSize << Field->getDeclName() << FieldBits;
2996 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002997 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002998 diag::note_transparent_union_first_field_size_align)
2999 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003000 return;
3001 }
3002 }
3003
Michael Han99315932013-01-24 16:46:58 +00003004 RD->addAttr(::new (S.Context)
3005 TransparentUnionAttr(Attr.getRange(), S.Context,
3006 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003007}
3008
Chandler Carruthedc2c642011-07-02 00:01:44 +00003009static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003010 // Make sure that there is a string literal as the annotation's single
3011 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003012 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003013 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003014 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003015
3016 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003017 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3018 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003019 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003020 }
Michael Han99315932013-01-24 16:46:58 +00003021
3022 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003023 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003024 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003025}
3026
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003027static void handleAlignValueAttr(Sema &S, Decl *D,
3028 const AttributeList &Attr) {
3029 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3030 Attr.getAttributeSpellingListIndex());
3031}
3032
3033void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3034 unsigned SpellingListIndex) {
3035 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3036 SourceLocation AttrLoc = AttrRange.getBegin();
3037
3038 QualType T;
3039 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3040 T = TD->getUnderlyingType();
3041 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3042 T = VD->getType();
3043 else
3044 llvm_unreachable("Unknown decl type for align_value");
3045
3046 if (!T->isDependentType() && !T->isAnyPointerType() &&
3047 !T->isReferenceType() && !T->isMemberPointerType()) {
3048 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3049 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3050 return;
3051 }
3052
3053 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003054 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003055 ExprResult ICE
3056 = VerifyIntegerConstantExpression(E, &Alignment,
3057 diag::err_align_value_attribute_argument_not_int,
3058 /*AllowFold*/ false);
3059 if (ICE.isInvalid())
3060 return;
3061
3062 if (!Alignment.isPowerOf2()) {
3063 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3064 << E->getSourceRange();
3065 return;
3066 }
3067
3068 D->addAttr(::new (Context)
3069 AlignValueAttr(AttrRange, Context, ICE.get(),
3070 SpellingListIndex));
3071 return;
3072 }
3073
3074 // Save dependent expressions in the AST to be instantiated.
3075 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
3076 return;
3077}
3078
Chandler Carruthedc2c642011-07-02 00:01:44 +00003079static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003080 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003081 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003082 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3083 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003084 return;
3085 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003086
Richard Smith848e1f12013-02-01 08:12:08 +00003087 if (Attr.getNumArgs() == 0) {
3088 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003089 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003090 return;
3091 }
3092
Aaron Ballman00e99962013-08-31 01:11:41 +00003093 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003094 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3095 S.Diag(Attr.getEllipsisLoc(),
3096 diag::err_pack_expansion_without_parameter_packs);
3097 return;
3098 }
3099
3100 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3101 return;
3102
David Majnemer26a1e0e2015-04-07 02:37:09 +00003103 if (E->isValueDependent()) {
3104 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3105 if (!TND->getUnderlyingType()->isDependentType()) {
3106 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3107 << E->getSourceRange();
3108 return;
3109 }
3110 }
3111 }
3112
Richard Smith44c247f2013-02-22 08:32:16 +00003113 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3114 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003115}
3116
3117void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003118 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003119 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3120 SourceLocation AttrLoc = AttrRange.getBegin();
3121
Richard Smith1dba27c2013-01-29 09:02:09 +00003122 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003123 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003124 // C++11 [dcl.align]p1:
3125 // An alignment-specifier may be applied to a variable or to a class
3126 // data member, but it shall not be applied to a bit-field, a function
3127 // parameter, the formal parameter of a catch clause, or a variable
3128 // declared with the register storage class specifier. An
3129 // alignment-specifier may also be applied to the declaration of a class
3130 // or enumeration type.
3131 // C11 6.7.5/2:
3132 // An alignment attribute shall not be specified in a declaration of
3133 // a typedef, or a bit-field, or a function, or a parameter, or an
3134 // object declared with the register storage-class specifier.
3135 int DiagKind = -1;
3136 if (isa<ParmVarDecl>(D)) {
3137 DiagKind = 0;
3138 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3139 if (VD->getStorageClass() == SC_Register)
3140 DiagKind = 1;
3141 if (VD->isExceptionVariable())
3142 DiagKind = 2;
3143 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3144 if (FD->isBitField())
3145 DiagKind = 3;
3146 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003147 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003148 << (TmpAttr.isC11() ? ExpectedVariableOrField
3149 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003150 return;
3151 }
3152 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003153 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003154 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003155 return;
3156 }
3157 }
3158
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003159 if (E->isTypeDependent() || E->isValueDependent()) {
3160 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003161 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3162 AA->setPackExpansion(IsPackExpansion);
3163 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003164 return;
3165 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003166
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003167 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003168 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003169 ExprResult ICE
3170 = VerifyIntegerConstantExpression(E, &Alignment,
3171 diag::err_aligned_attribute_argument_not_int,
3172 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003173 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003174 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003175
David Majnemer0be6bd02015-07-26 09:02:21 +00003176 uint64_t AlignVal = Alignment.getZExtValue();
3177
Richard Smith848e1f12013-02-01 08:12:08 +00003178 // C++11 [dcl.align]p2:
3179 // -- if the constant expression evaluates to zero, the alignment
3180 // specifier shall have no effect
3181 // C11 6.7.5p6:
3182 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003183 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003184 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003185 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3186 << E->getSourceRange();
3187 return;
3188 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003189 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003190
David Majnemerabecae72014-02-12 20:36:10 +00003191 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003192 unsigned MaxValidAlignment =
3193 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3194 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003195 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003196 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3197 << E->getSourceRange();
3198 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003199 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003200
David Majnemer0be6bd02015-07-26 09:02:21 +00003201 if (Context.getTargetInfo().isTLSSupported()) {
3202 unsigned MaxTLSAlign =
3203 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3204 .getQuantity();
3205 auto *VD = dyn_cast<VarDecl>(D);
3206 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3207 VD->getTLSKind() != VarDecl::TLS_None) {
3208 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3209 << (unsigned)AlignVal << VD << MaxTLSAlign;
3210 return;
3211 }
3212 }
3213
Richard Smith44c247f2013-02-22 08:32:16 +00003214 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003215 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003216 AA->setPackExpansion(IsPackExpansion);
3217 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003218}
3219
Michael Hanaf02bbe2013-02-01 01:19:17 +00003220void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003221 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003222 // FIXME: Cache the number on the Attr object if non-dependent?
3223 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003224 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3225 SpellingListIndex);
3226 AA->setPackExpansion(IsPackExpansion);
3227 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003228}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003229
Richard Smith848e1f12013-02-01 08:12:08 +00003230void Sema::CheckAlignasUnderalignment(Decl *D) {
3231 assert(D->hasAttrs() && "no attributes on decl");
3232
David Majnemer475b25e2015-01-21 10:54:38 +00003233 QualType UnderlyingTy, DiagTy;
3234 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3235 UnderlyingTy = DiagTy = VD->getType();
3236 } else {
3237 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3238 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3239 UnderlyingTy = ED->getIntegerType();
3240 }
3241 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003242 return;
3243
3244 // C++11 [dcl.align]p5, C11 6.7.5/4:
3245 // The combined effect of all alignment attributes in a declaration shall
3246 // not specify an alignment that is less strict than the alignment that
3247 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003248 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003249 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003250 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003251 if (I->isAlignmentDependent())
3252 return;
3253 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003254 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003255 Align = std::max(Align, I->getAlignment(Context));
3256 }
3257
3258 if (AlignasAttr && Align) {
3259 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003260 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003261 if (NaturalAlign > RequestedAlign)
3262 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003263 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003264 }
3265}
3266
David Majnemer2c4e00a2014-01-29 22:07:36 +00003267bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003268 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003269 MSInheritanceAttr::Spelling SemanticSpelling) {
3270 assert(RD->hasDefinition() && "RD has no definition!");
3271
David Majnemer98c9ee22014-02-07 00:43:07 +00003272 // We may not have seen base specifiers or any virtual methods yet. We will
3273 // have to wait until the record is defined to catch any mismatches.
3274 if (!RD->getDefinition()->isCompleteDefinition())
3275 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003276
David Majnemer98c9ee22014-02-07 00:43:07 +00003277 // The unspecified model never matches what a definition could need.
3278 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3279 return false;
3280
David Majnemer4bb09802014-02-10 19:50:15 +00003281 if (BestCase) {
3282 if (RD->calculateInheritanceModel() == SemanticSpelling)
3283 return false;
3284 } else {
3285 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3286 return false;
3287 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003288
3289 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3290 << 0 /*definition*/;
3291 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3292 << RD->getNameAsString();
3293 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003294}
3295
Alexey Bataevf278eb12015-11-19 10:13:11 +00003296/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3297/// attribute.
3298static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3299 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003300 IntegerMode = true;
3301 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003302 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003303 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003304 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003305 case 'Q':
3306 DestWidth = 8;
3307 break;
3308 case 'H':
3309 DestWidth = 16;
3310 break;
3311 case 'S':
3312 DestWidth = 32;
3313 break;
3314 case 'D':
3315 DestWidth = 64;
3316 break;
3317 case 'X':
3318 DestWidth = 96;
3319 break;
3320 case 'T':
3321 DestWidth = 128;
3322 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003323 }
3324 if (Str[1] == 'F') {
3325 IntegerMode = false;
3326 } else if (Str[1] == 'C') {
3327 IntegerMode = false;
3328 ComplexMode = true;
3329 } else if (Str[1] != 'I') {
3330 DestWidth = 0;
3331 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003332 break;
3333 case 4:
3334 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3335 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003336 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003337 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003338 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003339 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003340 break;
3341 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003342 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003343 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003344 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003345 case 11:
3346 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003347 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003348 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003349 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003350}
3351
3352/// handleModeAttr - This attribute modifies the width of a decl with primitive
3353/// type.
3354///
3355/// Despite what would be logical, the mode attribute is a decl attribute, not a
3356/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3357/// HImode, not an intermediate pointer.
3358static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3359 // This attribute isn't documented, but glibc uses it. It changes
3360 // the width of an int or unsigned int to the specified size.
3361 if (!Attr.isArgIdent(0)) {
3362 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3363 << AANT_ArgumentIdentifier;
3364 return;
3365 }
3366
3367 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003368
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003369 S.AddModeAttr(Attr.getRange(), D, Name, Attr.getAttributeSpellingListIndex());
3370}
3371
3372void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3373 unsigned SpellingListIndex, bool InInstantiation) {
3374 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003375 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003376 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003377
3378 unsigned DestWidth = 0;
3379 bool IntegerMode = true;
3380 bool ComplexMode = false;
3381 llvm::APInt VectorSize(64, 0);
3382 if (Str.size() >= 4 && Str[0] == 'V') {
3383 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3384 size_t StrSize = Str.size();
3385 size_t VectorStringLength = 0;
3386 while ((VectorStringLength + 1) < StrSize &&
3387 isdigit(Str[VectorStringLength + 1]))
3388 ++VectorStringLength;
3389 if (VectorStringLength &&
3390 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3391 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003392 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003393 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003394 // Avoid duplicate warning from template instantiation.
3395 if (!InInstantiation)
3396 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003397 } else {
3398 VectorSize = 0;
3399 }
3400 }
3401
3402 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003403 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3404
3405 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3406 // and friends, at least with glibc.
3407 // FIXME: Make sure floating-point mappings are accurate
3408 // FIXME: Support XF and TF types
3409 if (!DestWidth) {
3410 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3411 return;
3412 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003413
3414 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003415 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003416 OldTy = TD->getUnderlyingType();
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003417 else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
3418 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3419 // Try to get type from enum declaration, default to int.
3420 OldTy = ED->getIntegerType();
3421 if (OldTy.isNull())
3422 OldTy = Context.IntTy;
3423 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003424 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003425
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003426 if (OldTy->isDependentType()) {
3427 D->addAttr(::new (Context)
3428 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3429 return;
3430 }
3431
Alexey Bataev326057d2015-06-19 07:46:21 +00003432 // Base type can also be a vector type (see PR17453).
3433 // Distinguish between base type and base element type.
3434 QualType OldElemTy = OldTy;
3435 if (const VectorType *VT = OldTy->getAs<VectorType>())
3436 OldElemTy = VT->getElementType();
3437
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003438 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3439 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3440 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3441 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3442 VectorSize.getBoolValue()) {
3443 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3444 return;
3445 }
3446 bool IntegralOrAnyEnumType =
3447 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3448
3449 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3450 !IntegralOrAnyEnumType)
3451 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003452 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003453 if (!IntegralOrAnyEnumType)
3454 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003455 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003456 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003457 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003458 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003459 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003460 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003461 }
3462
Alexey Bataev326057d2015-06-19 07:46:21 +00003463 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003464
3465 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003466 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3467 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003468 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003469 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003470
Alexey Bataev326057d2015-06-19 07:46:21 +00003471 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003472 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003473 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003474 }
3475
Eli Friedman4735374e2009-03-03 06:41:03 +00003476 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003477 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003478 }
3479
3480 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003481 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003482 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3483 VectorType::GenericVector);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003484 } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003485 // Complex machine mode does not support base vector types.
3486 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003487 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003488 return;
3489 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003490 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003491 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003492 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003493 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003494 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003495 }
3496
3497 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003498 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003499 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003500 }
3501
3502 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003503 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3504 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003505 else if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3506 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003507 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003508 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003509
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003510 D->addAttr(::new (Context)
3511 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003512}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003513
Chandler Carruthedc2c642011-07-02 00:01:44 +00003514static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003515 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3516 if (!VD->hasGlobalStorage())
3517 S.Diag(Attr.getLoc(),
3518 diag::warn_attribute_requires_functions_or_static_globals)
3519 << Attr.getName();
3520 } else if (!isFunctionOrMethod(D)) {
3521 S.Diag(Attr.getLoc(),
3522 diag::warn_attribute_requires_functions_or_static_globals)
3523 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003524 return;
3525 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003526
Michael Han99315932013-01-24 16:46:58 +00003527 D->addAttr(::new (S.Context)
3528 NoDebugAttr(Attr.getRange(), S.Context,
3529 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003530}
3531
Paul Robinson30e41fb2014-12-15 18:57:28 +00003532AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003533 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003534 unsigned AttrSpellingListIndex) {
3535 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003536 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003537 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3538 return nullptr;
3539 }
3540
3541 if (D->hasAttr<AlwaysInlineAttr>())
3542 return nullptr;
3543
3544 return ::new (Context) AlwaysInlineAttr(Range, Context,
3545 AttrSpellingListIndex);
3546}
3547
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003548CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3549 IdentifierInfo *Ident,
3550 unsigned AttrSpellingListIndex) {
3551 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3552 return nullptr;
3553
3554 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3555}
3556
3557InternalLinkageAttr *
3558Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3559 IdentifierInfo *Ident,
3560 unsigned AttrSpellingListIndex) {
3561 if (auto VD = dyn_cast<VarDecl>(D)) {
3562 // Attribute applies to Var but not any subclass of it (like ParmVar,
3563 // ImplicitParm or VarTemplateSpecialization).
3564 if (VD->getKind() != Decl::Var) {
3565 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3566 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3567 : ExpectedVariableOrFunction);
3568 return nullptr;
3569 }
3570 // Attribute does not apply to non-static local variables.
3571 if (VD->hasLocalStorage()) {
3572 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3573 return nullptr;
3574 }
3575 }
3576
3577 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3578 return nullptr;
3579
3580 return ::new (Context)
3581 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3582}
3583
Paul Robinson30e41fb2014-12-15 18:57:28 +00003584MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3585 unsigned AttrSpellingListIndex) {
3586 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3587 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3588 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3589 return nullptr;
3590 }
3591
3592 if (D->hasAttr<MinSizeAttr>())
3593 return nullptr;
3594
3595 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3596}
3597
3598OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3599 unsigned AttrSpellingListIndex) {
3600 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3601 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3602 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3603 D->dropAttr<AlwaysInlineAttr>();
3604 }
3605 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3606 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3607 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3608 D->dropAttr<MinSizeAttr>();
3609 }
3610
3611 if (D->hasAttr<OptimizeNoneAttr>())
3612 return nullptr;
3613
3614 return ::new (Context) OptimizeNoneAttr(Range, Context,
3615 AttrSpellingListIndex);
3616}
3617
Paul Robinsonf0674352014-03-31 22:29:15 +00003618static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3619 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003620 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(),
3621 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003622 return;
3623
Paul Robinson080b1f32015-01-13 18:34:56 +00003624 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3625 D, Attr.getRange(), Attr.getName(),
3626 Attr.getAttributeSpellingListIndex()))
3627 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003628}
3629
Paul Robinson080b1f32015-01-13 18:34:56 +00003630static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3631 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3632 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3633 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003634}
3635
Paul Robinsonf0674352014-03-31 22:29:15 +00003636static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3637 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003638 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3639 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3640 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003641}
3642
Chandler Carruthedc2c642011-07-02 00:01:44 +00003643static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00003644 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(),
3645 Attr.getName()) ||
3646 checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(),
3647 Attr.getName())) {
3648 return;
3649 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003650 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003651 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003652 SourceRange RTRange = FD->getReturnTypeSourceRange();
3653 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003654 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003655 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3656 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003657 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003658 }
Justin Lebarc66a1062016-01-20 00:26:57 +00003659 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
3660 if (Method->isInstance()) {
3661 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
3662 << Method;
3663 return;
3664 }
3665 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
3666 }
3667 // Only warn for "inline" when compiling for host, to cut down on noise.
3668 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
3669 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003670
Aaron Ballman3aff6332013-12-02 19:30:36 +00003671 D->addAttr(::new (S.Context)
3672 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003673 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003674}
3675
Chandler Carruthedc2c642011-07-02 00:01:44 +00003676static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003677 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003678 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003679 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003680 return;
3681 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003682
Michael Han99315932013-01-24 16:46:58 +00003683 D->addAttr(::new (S.Context)
3684 GNUInlineAttr(Attr.getRange(), S.Context,
3685 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003686}
3687
Chandler Carruthedc2c642011-07-02 00:01:44 +00003688static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003689 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003690
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003691 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003692 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3693 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003694 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003695 return;
3696
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003697 if (!isa<ObjCMethodDecl>(D)) {
3698 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3699 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003700 return;
3701 }
3702
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003703 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003704 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003705 D->addAttr(::new (S.Context)
3706 FastCallAttr(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_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003710 D->addAttr(::new (S.Context)
3711 StdCallAttr(Attr.getRange(), S.Context,
3712 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003713 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003714 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003715 D->addAttr(::new (S.Context)
3716 ThisCallAttr(Attr.getRange(), S.Context,
3717 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003718 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003719 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003720 D->addAttr(::new (S.Context)
3721 CDeclAttr(Attr.getRange(), S.Context,
3722 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003723 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003724 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003725 D->addAttr(::new (S.Context)
3726 PascalAttr(Attr.getRange(), S.Context,
3727 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003728 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003729 case AttributeList::AT_VectorCall:
3730 D->addAttr(::new (S.Context)
3731 VectorCallAttr(Attr.getRange(), S.Context,
3732 Attr.getAttributeSpellingListIndex()));
3733 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003734 case AttributeList::AT_MSABI:
3735 D->addAttr(::new (S.Context)
3736 MSABIAttr(Attr.getRange(), S.Context,
3737 Attr.getAttributeSpellingListIndex()));
3738 return;
3739 case AttributeList::AT_SysVABI:
3740 D->addAttr(::new (S.Context)
3741 SysVABIAttr(Attr.getRange(), S.Context,
3742 Attr.getAttributeSpellingListIndex()));
3743 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003744 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003745 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003746 switch (CC) {
3747 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003748 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003749 break;
3750 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003751 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003752 break;
3753 default:
3754 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003755 }
3756
Michael Han99315932013-01-24 16:46:58 +00003757 D->addAttr(::new (S.Context)
3758 PcsAttr(Attr.getRange(), S.Context, PCS,
3759 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003760 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003761 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003762 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003763 D->addAttr(::new (S.Context)
3764 IntelOclBiccAttr(Attr.getRange(), S.Context,
3765 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003766 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003767
Abramo Bagnara50099372010-04-30 13:10:51 +00003768 default:
3769 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003770 }
3771}
3772
Aaron Ballman02df2e02012-12-09 17:45:41 +00003773bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3774 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003775 if (attr.isInvalid())
3776 return true;
3777
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003778 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003779 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003780 attr.setInvalid();
3781 return true;
3782 }
3783
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003784 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003785 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003786 case AttributeList::AT_CDecl: CC = CC_C; break;
3787 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3788 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3789 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3790 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003791 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003792 case AttributeList::AT_MSABI:
3793 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3794 CC_X86_64Win64;
3795 break;
3796 case AttributeList::AT_SysVABI:
3797 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3798 CC_C;
3799 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003800 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003801 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003802 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003803 attr.setInvalid();
3804 return true;
3805 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003806 if (StrRef == "aapcs") {
3807 CC = CC_AAPCS;
3808 break;
3809 } else if (StrRef == "aapcs-vfp") {
3810 CC = CC_AAPCS_VFP;
3811 break;
3812 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003813
3814 attr.setInvalid();
3815 Diag(attr.getLoc(), diag::err_invalid_pcs);
3816 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003817 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003818 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003819 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003820 }
3821
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003822 const TargetInfo &TI = Context.getTargetInfo();
3823 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003824 if (A != TargetInfo::CCCR_OK) {
3825 if (A == TargetInfo::CCCR_Warning)
3826 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003827
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003828 // This convention is not valid for the target. Use the default function or
3829 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003830 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3831 if (FD)
3832 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3833 TargetInfo::CCMT_NonMember;
3834 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003835 }
3836
John McCall3882ace2011-01-05 12:14:39 +00003837 return false;
3838}
3839
John McCall3882ace2011-01-05 12:14:39 +00003840/// Checks a regparm attribute, returning true if it is ill-formed and
3841/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003842bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3843 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003844 return true;
3845
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003846 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003847 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003848 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003849 }
Eli Friedman7044b762009-03-27 21:06:47 +00003850
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003851 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003852 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003853 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003854 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003855 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003856 }
3857
Douglas Gregore8bbc122011-09-02 00:18:52 +00003858 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003859 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003860 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003861 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003862 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003863 }
3864
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003865 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003866 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003867 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003868 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003869 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003870 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003871 }
3872
John McCall3882ace2011-01-05 12:14:39 +00003873 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003874}
3875
Artem Belevich7093e402015-04-21 22:55:54 +00003876// Checks whether an argument of launch_bounds attribute is acceptable
3877// May output an error.
3878static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3879 const CUDALaunchBoundsAttr &Attr,
3880 const unsigned Idx) {
3881
3882 if (S.DiagnoseUnexpandedParameterPack(E))
3883 return false;
3884
3885 // Accept template arguments for now as they depend on something else.
3886 // We'll get to check them when they eventually get instantiated.
3887 if (E->isValueDependent())
3888 return true;
3889
3890 llvm::APSInt I(64);
3891 if (!E->isIntegerConstantExpr(I, S.Context)) {
3892 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3893 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3894 return false;
3895 }
3896 // Make sure we can fit it in 32 bits.
3897 if (!I.isIntN(32)) {
3898 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3899 << 32 << /* Unsigned */ 1;
3900 return false;
3901 }
3902 if (I < 0)
3903 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3904 << &Attr << Idx << E->getSourceRange();
3905
3906 return true;
3907}
3908
3909void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3910 Expr *MinBlocks, unsigned SpellingListIndex) {
3911 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3912 SpellingListIndex);
3913
3914 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003915 return;
3916
Artem Belevich7093e402015-04-21 22:55:54 +00003917 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3918 return;
3919
3920 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3921 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3922}
3923
3924static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3925 const AttributeList &Attr) {
3926 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3927 !checkAttributeAtMostNumArgs(S, Attr, 2))
3928 return;
3929
3930 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3931 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3932 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003933}
3934
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003935static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3936 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003937 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003938 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003939 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003940 return;
3941 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003942
3943 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003944 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003945
Aaron Ballman00e99962013-08-31 01:11:41 +00003946 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003947
3948 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3949 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3950 << Attr.getName() << ExpectedFunctionOrMethod;
3951 return;
3952 }
3953
3954 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003955 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3956 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003957 return;
3958
3959 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003960 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3961 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003962 return;
3963
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003964 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003965 if (IsPointer) {
3966 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003967 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003968 if (!BufferTy->isPointerType()) {
3969 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00003970 << Attr.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003971 }
3972 }
3973
Michael Han99315932013-01-24 16:46:58 +00003974 D->addAttr(::new (S.Context)
3975 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3976 ArgumentIdx, TypeTagIdx, IsPointer,
3977 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003978}
3979
3980static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3981 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003982 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003983 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003984 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003985 return;
3986 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003987
3988 if (!checkAttributeNumArgs(S, Attr, 1))
3989 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003990
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003991 if (!isa<VarDecl>(D)) {
3992 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3993 << Attr.getName() << ExpectedVariable;
3994 return;
3995 }
3996
Aaron Ballman00e99962013-08-31 01:11:41 +00003997 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003998 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003999 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4000 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004001
Michael Han99315932013-01-24 16:46:58 +00004002 D->addAttr(::new (S.Context)
4003 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004004 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00004005 Attr.getLayoutCompatible(),
4006 Attr.getMustBeNull(),
4007 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004008}
4009
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004010//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004011// Checker-specific attribute handlers.
4012//===----------------------------------------------------------------------===//
4013
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004014static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004015 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004016 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004017}
4018
John McCalled433932011-01-25 03:31:58 +00004019static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004020 return type->isDependentType() ||
4021 type->isObjCObjectPointerType() ||
4022 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004023}
4024static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004025 return type->isDependentType() ||
4026 type->isPointerType() ||
4027 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004028}
4029
Chandler Carruthedc2c642011-07-02 00:01:44 +00004030static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004031 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004032 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004033
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004034 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004035 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4036 cf = false;
4037 } else {
4038 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4039 cf = true;
4040 }
4041
4042 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004043 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004044 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004045 return;
4046 }
4047
4048 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004049 param->addAttr(::new (S.Context)
4050 CFConsumedAttr(Attr.getRange(), S.Context,
4051 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004052 else
Michael Han99315932013-01-24 16:46:58 +00004053 param->addAttr(::new (S.Context)
4054 NSConsumedAttr(Attr.getRange(), S.Context,
4055 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004056}
4057
Chandler Carruthedc2c642011-07-02 00:01:44 +00004058static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4059 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004060
John McCalled433932011-01-25 03:31:58 +00004061 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004062
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004063 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004064 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004065 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004066 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004067 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004068 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4069 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004070 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004071 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004072 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
4073 returnType = Param->getType()->getPointeeType();
4074 if (returnType.isNull()) {
4075 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4076 << Attr.getName() << /*pointer-to-CF*/2
4077 << Attr.getRange();
4078 return;
4079 }
4080 } else {
4081 AttributeDeclKind ExpectedDeclKind;
4082 switch (Attr.getKind()) {
4083 default: llvm_unreachable("invalid ownership attribute");
4084 case AttributeList::AT_NSReturnsRetained:
4085 case AttributeList::AT_NSReturnsAutoreleased:
4086 case AttributeList::AT_NSReturnsNotRetained:
4087 ExpectedDeclKind = ExpectedFunctionOrMethod;
4088 break;
4089
4090 case AttributeList::AT_CFReturnsRetained:
4091 case AttributeList::AT_CFReturnsNotRetained:
4092 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4093 break;
4094 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004095 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004096 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004097 return;
4098 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004099
John McCalled433932011-01-25 03:31:58 +00004100 bool typeOK;
4101 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004102 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004103 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004104 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004105 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004106 cf = false;
4107 break;
4108
4109 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004110 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004111 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4112 cf = false;
4113 break;
4114
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004115 case AttributeList::AT_CFReturnsRetained:
4116 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004117 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4118 cf = true;
4119 break;
4120 }
4121
4122 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004123 if (isa<ParmVarDecl>(D)) {
4124 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4125 << Attr.getName() << /*pointer-to-CF*/2
4126 << Attr.getRange();
4127 } else {
4128 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4129 enum : unsigned {
4130 Function,
4131 Method,
4132 Property
4133 } SubjectKind = Function;
4134 if (isa<ObjCMethodDecl>(D))
4135 SubjectKind = Method;
4136 else if (isa<ObjCPropertyDecl>(D))
4137 SubjectKind = Property;
4138 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4139 << Attr.getName() << SubjectKind << cf
4140 << Attr.getRange();
4141 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004142 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004143 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004144
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004145 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004146 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004147 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004148 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004149 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
4150 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004151 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004152 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004153 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
4154 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004155 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004156 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004157 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
4158 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004159 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004160 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004161 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
4162 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004163 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004164 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004165 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
4166 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004167 return;
4168 };
4169}
4170
John McCallcf166702011-07-22 08:53:00 +00004171static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4172 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004173 const int EP_ObjCMethod = 1;
4174 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004175
John McCallcf166702011-07-22 08:53:00 +00004176 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004177 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004178 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004179 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004180 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004181 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004182
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004183 if (!resultType->isReferenceType() &&
4184 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004185 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004186 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004187 << attr.getName()
4188 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004189 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004190
4191 // Drop the attribute.
4192 return;
4193 }
4194
Nico Weber462fd1e2015-01-07 23:50:05 +00004195 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
4196 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004197}
4198
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004199static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4200 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004201 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004202
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004203 DeclContext *DC = method->getDeclContext();
4204 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4205 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4206 << attr.getName() << 0;
4207 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4208 return;
4209 }
4210 if (method->getMethodFamily() == OMF_dealloc) {
4211 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4212 << attr.getName() << 1;
4213 return;
4214 }
4215
Michael Han99315932013-01-24 16:46:58 +00004216 method->addAttr(::new (S.Context)
4217 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4218 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004219}
4220
Aaron Ballmanfb763042013-12-02 18:05:46 +00004221static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4222 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004223 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(),
4224 Attr.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004225 return;
John McCall32f5fe12011-09-30 05:12:12 +00004226
Aaron Ballmanfb763042013-12-02 18:05:46 +00004227 D->addAttr(::new (S.Context)
4228 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4229 Attr.getAttributeSpellingListIndex()));
4230}
4231
4232static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4233 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004234 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(),
4235 Attr.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004236 return;
4237
4238 D->addAttr(::new (S.Context)
4239 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4240 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004241}
4242
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004243static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4244 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004245 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004246
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004247 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004248 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004249 return;
4250 }
John McCall28592582015-02-01 22:34:06 +00004251
4252 // Typedefs only allow objc_bridge(id) and have some additional checking.
4253 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4254 if (!Parm->Ident->isStr("id")) {
4255 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4256 << Attr.getName();
4257 return;
4258 }
4259
4260 // Only allow 'cv void *'.
4261 QualType T = TD->getUnderlyingType();
4262 if (!T->isVoidPointerType()) {
4263 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4264 return;
4265 }
4266 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004267
4268 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004269 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004270 Attr.getAttributeSpellingListIndex()));
4271}
4272
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004273static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4274 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004275 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4276
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004277 if (!Parm) {
4278 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4279 return;
4280 }
4281
4282 D->addAttr(::new (S.Context)
4283 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4284 Attr.getAttributeSpellingListIndex()));
4285}
4286
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004287static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4288 const AttributeList &Attr) {
4289 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004290 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004291 if (!RelatedClass) {
4292 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4293 return;
4294 }
4295 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004296 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004297 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004298 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004299 D->addAttr(::new (S.Context)
4300 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4301 ClassMethod, InstanceMethod,
4302 Attr.getAttributeSpellingListIndex()));
4303}
4304
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004305static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4306 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004307 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004308 if (ObjCCategoryDecl *CatDecl =
4309 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004310 IFace = CatDecl->getClassInterface();
4311 else
4312 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004313
4314 if (!IFace)
4315 return;
4316
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004317 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004318 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004319 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4320 Attr.getAttributeSpellingListIndex()));
4321}
4322
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004323static void handleObjCRuntimeName(Sema &S, Decl *D,
4324 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004325 StringRef MetaDataName;
4326 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4327 return;
4328 D->addAttr(::new (S.Context)
4329 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4330 MetaDataName,
4331 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004332}
4333
Alex Denisovfde64952015-06-26 05:28:36 +00004334// when a user wants to use objc_boxable with a union or struct
4335// but she doesn't have access to the declaration (legacy/third-party code)
4336// then she can 'enable' this feature via trick with a typedef
4337// e.g.:
4338// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4339static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4340 bool notify = false;
4341
4342 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4343 if (RD && RD->getDefinition()) {
4344 RD = RD->getDefinition();
4345 notify = true;
4346 }
4347
4348 if (RD) {
4349 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4350 ObjCBoxableAttr(Attr.getRange(), S.Context,
4351 Attr.getAttributeSpellingListIndex());
4352 RD->addAttr(BoxableAttr);
4353 if (notify) {
4354 // we need to notify ASTReader/ASTWriter about
4355 // modification of existing declaration
4356 if (ASTMutationListener *L = S.getASTMutationListener())
4357 L->AddedAttributeToRecord(BoxableAttr, RD);
4358 }
4359 }
4360}
4361
Chandler Carruthedc2c642011-07-02 00:01:44 +00004362static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4363 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004364 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004365
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004366 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004367 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004368}
4369
Chandler Carruthedc2c642011-07-02 00:01:44 +00004370static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4371 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004372 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004373 QualType type = vd->getType();
4374
4375 if (!type->isDependentType() &&
4376 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004377 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004378 << type;
4379 return;
4380 }
4381
4382 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4383
4384 // If we have no lifetime yet, check the lifetime we're presumably
4385 // going to infer.
4386 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4387 lifetime = type->getObjCARCImplicitLifetime();
4388
4389 switch (lifetime) {
4390 case Qualifiers::OCL_None:
4391 assert(type->isDependentType() &&
4392 "didn't infer lifetime for non-dependent type?");
4393 break;
4394
4395 case Qualifiers::OCL_Weak: // meaningful
4396 case Qualifiers::OCL_Strong: // meaningful
4397 break;
4398
4399 case Qualifiers::OCL_ExplicitNone:
4400 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004401 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004402 << (lifetime == Qualifiers::OCL_Autoreleasing);
4403 break;
4404 }
4405
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004406 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004407 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4408 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004409}
4410
Francois Picheta83957a2010-12-19 06:50:37 +00004411//===----------------------------------------------------------------------===//
4412// Microsoft specific attribute handlers.
4413//===----------------------------------------------------------------------===//
4414
Chandler Carruthedc2c642011-07-02 00:01:44 +00004415static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004416 if (!S.LangOpts.CPlusPlus) {
4417 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4418 << Attr.getName() << AttributeLangSupport::C;
4419 return;
4420 }
4421
Aaron Ballman60e705e2013-11-24 20:58:02 +00004422 if (!isa<CXXRecordDecl>(D)) {
4423 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4424 << Attr.getName() << ExpectedClass;
4425 return;
4426 }
4427
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004428 StringRef StrRef;
4429 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004430 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004431 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004432
David Majnemer89085342013-08-09 08:56:20 +00004433 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4434 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004435 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4436 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004437
Reid Kleckner140c4a72013-05-17 14:04:52 +00004438 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004439 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004440 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004441 return;
4442 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004443
David Majnemer89085342013-08-09 08:56:20 +00004444 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004445 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004446 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004447 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004448 return;
4449 }
David Majnemer89085342013-08-09 08:56:20 +00004450 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004451 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004452 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004453 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004454 }
Francois Picheta83957a2010-12-19 06:50:37 +00004455
David Majnemer89085342013-08-09 08:56:20 +00004456 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4457 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004458}
4459
David Majnemer2c4e00a2014-01-29 22:07:36 +00004460static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4461 if (!S.LangOpts.CPlusPlus) {
4462 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4463 << Attr.getName() << AttributeLangSupport::C;
4464 return;
4465 }
4466 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004467 D, Attr.getRange(), /*BestCase=*/true,
4468 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004469 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00004470 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00004471 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00004472 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
4473 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00004474}
4475
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004476static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4477 const AttributeList &Attr) {
4478 VarDecl *VD = cast<VarDecl>(D);
4479 if (!S.Context.getTargetInfo().isTLSSupported()) {
4480 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4481 return;
4482 }
4483 if (VD->getTSCSpec() != TSCS_unspecified) {
4484 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4485 return;
4486 }
4487 if (VD->hasLocalStorage()) {
4488 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4489 return;
4490 }
4491 VD->addAttr(::new (S.Context) ThreadAttr(
4492 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4493}
4494
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004495static void handleARMInterruptAttr(Sema &S, Decl *D,
4496 const AttributeList &Attr) {
4497 // Check the attribute arguments.
4498 if (Attr.getNumArgs() > 1) {
4499 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4500 << Attr.getName() << 1;
4501 return;
4502 }
4503
4504 StringRef Str;
4505 SourceLocation ArgLoc;
4506
4507 if (Attr.getNumArgs() == 0)
4508 Str = "";
4509 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4510 return;
4511
4512 ARMInterruptAttr::InterruptType Kind;
4513 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4514 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4515 << Attr.getName() << Str << ArgLoc;
4516 return;
4517 }
4518
4519 unsigned Index = Attr.getAttributeSpellingListIndex();
4520 D->addAttr(::new (S.Context)
4521 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4522}
4523
4524static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4525 const AttributeList &Attr) {
4526 if (!checkAttributeNumArgs(S, Attr, 1))
4527 return;
4528
4529 if (!Attr.isArgExpr(0)) {
4530 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4531 << AANT_ArgumentIntegerConstant;
4532 return;
4533 }
4534
4535 // FIXME: Check for decl - it should be void ()(void).
4536
4537 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4538 llvm::APSInt NumParams(32);
4539 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4540 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4541 << Attr.getName() << AANT_ArgumentIntegerConstant
4542 << NumParamsExpr->getSourceRange();
4543 return;
4544 }
4545
4546 unsigned Num = NumParams.getLimitedValue(255);
4547 if ((Num & 1) || Num > 30) {
4548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4549 << Attr.getName() << (int)NumParams.getSExtValue()
4550 << NumParamsExpr->getSourceRange();
4551 return;
4552 }
4553
Aaron Ballman36a53502014-01-16 13:03:14 +00004554 D->addAttr(::new (S.Context)
4555 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4556 Attr.getAttributeSpellingListIndex()));
4557 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004558}
4559
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004560static void handleMipsInterruptAttr(Sema &S, Decl *D,
4561 const AttributeList &Attr) {
4562 // Only one optional argument permitted.
4563 if (Attr.getNumArgs() > 1) {
4564 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4565 << Attr.getName() << 1;
4566 return;
4567 }
4568
4569 StringRef Str;
4570 SourceLocation ArgLoc;
4571
4572 if (Attr.getNumArgs() == 0)
4573 Str = "";
4574 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4575 return;
4576
4577 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
4578 // a) Must be a function.
4579 // b) Must have no parameters.
4580 // c) Must have the 'void' return type.
4581 // d) Cannot have the 'mips16' attribute, as that instruction set
4582 // lacks the 'eret' instruction.
4583 // e) The attribute itself must either have no argument or one of the
4584 // valid interrupt types, see [MipsInterruptDocs].
4585
4586 if (!isFunctionOrMethod(D)) {
4587 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
4588 << "'interrupt'" << ExpectedFunctionOrMethod;
4589 return;
4590 }
4591
4592 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
4593 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4594 << 0;
4595 return;
4596 }
4597
4598 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4599 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4600 << 1;
4601 return;
4602 }
4603
4604 if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(),
4605 Attr.getName()))
4606 return;
4607
4608 MipsInterruptAttr::InterruptType Kind;
4609 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4610 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4611 << Attr.getName() << "'" + std::string(Str) + "'";
4612 return;
4613 }
4614
4615 D->addAttr(::new (S.Context) MipsInterruptAttr(
4616 Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex()));
4617}
4618
Alexey Bataevd51e9932016-01-15 04:06:31 +00004619static void handleAnyX86InterruptAttr(Sema &S, Decl *D,
4620 const AttributeList &Attr) {
4621 // Semantic checks for a function with the 'interrupt' attribute.
4622 // a) Must be a function.
4623 // b) Must have the 'void' return type.
4624 // c) Must take 1 or 2 arguments.
4625 // d) The 1st argument must be a pointer.
4626 // e) The 2nd argument (if any) must be an unsigned integer.
4627 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
4628 CXXMethodDecl::isStaticOverloadedOperator(
4629 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
4630 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4631 << Attr.getName() << ExpectedFunctionWithProtoType;
4632 return;
4633 }
4634 // Interrupt handler must have void return type.
4635 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4636 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
4637 diag::err_anyx86_interrupt_attribute)
4638 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4639 ? 0
4640 : 1)
4641 << 0;
4642 return;
4643 }
4644 // Interrupt handler must have 1 or 2 parameters.
4645 unsigned NumParams = getFunctionOrMethodNumParams(D);
4646 if (NumParams < 1 || NumParams > 2) {
4647 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
4648 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4649 ? 0
4650 : 1)
4651 << 1;
4652 return;
4653 }
4654 // The first argument must be a pointer.
4655 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
4656 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
4657 diag::err_anyx86_interrupt_attribute)
4658 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4659 ? 0
4660 : 1)
4661 << 2;
4662 return;
4663 }
4664 // The second argument, if present, must be an unsigned integer.
4665 unsigned TypeSize =
4666 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
4667 ? 64
4668 : 32;
4669 if (NumParams == 2 &&
4670 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
4671 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
4672 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
4673 diag::err_anyx86_interrupt_attribute)
4674 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4675 ? 0
4676 : 1)
4677 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
4678 return;
4679 }
4680 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
4681 Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex()));
4682 D->addAttr(UsedAttr::CreateImplicit(S.Context));
4683}
4684
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004685static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4686 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00004687 switch (S.Context.getTargetInfo().getTriple().getArch()) {
4688 case llvm::Triple::msp430:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004689 handleMSP430InterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004690 break;
4691 case llvm::Triple::mipsel:
4692 case llvm::Triple::mips:
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004693 handleMipsInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004694 break;
4695 case llvm::Triple::x86:
4696 case llvm::Triple::x86_64:
4697 handleAnyX86InterruptAttr(S, D, Attr);
4698 break;
4699 default:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004700 handleARMInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004701 break;
4702 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004703}
4704
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004705static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4706 const AttributeList &Attr) {
4707 uint32_t NumRegs;
4708 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4709 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4710 return;
4711
4712 D->addAttr(::new (S.Context)
4713 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4714 NumRegs,
4715 Attr.getAttributeSpellingListIndex()));
4716}
4717
4718static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4719 const AttributeList &Attr) {
4720 uint32_t NumRegs;
4721 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4722 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4723 return;
4724
4725 D->addAttr(::new (S.Context)
4726 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4727 NumRegs,
4728 Attr.getAttributeSpellingListIndex()));
4729}
4730
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004731static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4732 const AttributeList& Attr) {
4733 // If we try to apply it to a function pointer, don't warn, but don't
4734 // do anything, either. It doesn't matter anyway, because there's nothing
4735 // special about calling a force_align_arg_pointer function.
4736 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4737 if (VD && VD->getType()->isFunctionPointerType())
4738 return;
4739 // Also don't warn on function pointer typedefs.
4740 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4741 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4742 TD->getUnderlyingType()->isFunctionType()))
4743 return;
4744 // Attribute can only be applied to function types.
4745 if (!isa<FunctionDecl>(D)) {
4746 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4747 << Attr.getName() << /* function */0;
4748 return;
4749 }
4750
Aaron Ballman36a53502014-01-16 13:03:14 +00004751 D->addAttr(::new (S.Context)
4752 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4753 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004754}
4755
4756DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4757 unsigned AttrSpellingListIndex) {
4758 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004759 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004760 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004761 }
4762
4763 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004764 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004765
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004766 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004767}
4768
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004769DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4770 unsigned AttrSpellingListIndex) {
4771 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004772 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004773 D->dropAttr<DLLImportAttr>();
4774 }
4775
4776 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004777 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004778
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004779 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004780}
4781
Hans Wennborge82f19c2014-06-24 23:57:05 +00004782static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004783 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4784 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4785 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4786 << A.getName();
4787 return;
4788 }
4789
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004790 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4791 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4792 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4793 // MinGW doesn't allow dllimport on inline functions.
4794 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4795 << A.getName();
4796 return;
4797 }
4798 }
4799
Hans Wennborg5869ec42015-09-15 21:05:30 +00004800 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4801 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
4802 MD->getParent()->isLambda()) {
4803 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
4804 return;
4805 }
4806 }
4807
Hans Wennborge82f19c2014-06-24 23:57:05 +00004808 unsigned Index = A.getAttributeSpellingListIndex();
4809 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4810 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4811 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004812 if (NewAttr)
4813 D->addAttr(NewAttr);
4814}
4815
David Majnemer2c4e00a2014-01-29 22:07:36 +00004816MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004817Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004818 unsigned AttrSpellingListIndex,
4819 MSInheritanceAttr::Spelling SemanticSpelling) {
4820 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4821 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004822 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004823 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4824 << 1 /*previous declaration*/;
4825 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4826 D->dropAttr<MSInheritanceAttr>();
4827 }
4828
4829 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4830 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004831 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4832 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004833 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004834 }
4835 } else {
4836 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4837 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4838 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004839 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004840 }
4841 if (RD->getDescribedClassTemplate()) {
4842 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4843 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004844 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004845 }
4846 }
4847
4848 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004849 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004850}
4851
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004852static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4853 // The capability attributes take a single string parameter for the name of
4854 // the capability they represent. The lockable attribute does not take any
4855 // parameters. However, semantically, both attributes represent the same
4856 // concept, and so they use the same semantic attribute. Eventually, the
4857 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004858 //
Alp Toker958027b2014-07-14 19:42:55 +00004859 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004860 // literal will be considered a "mutex."
4861 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004862 SourceLocation LiteralLoc;
4863 if (Attr.getKind() == AttributeList::AT_Capability &&
4864 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4865 return;
4866
Aaron Ballman6c810072014-03-05 21:47:13 +00004867 // Currently, there are only two names allowed for a capability: role and
4868 // mutex (case insensitive). Diagnose other capability names.
4869 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4870 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4871
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004872 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4873 Attr.getAttributeSpellingListIndex()));
4874}
4875
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004876static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4877 const AttributeList &Attr) {
4878 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4879 Attr.getArgAsExpr(0),
4880 Attr.getAttributeSpellingListIndex()));
4881}
4882
4883static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4884 const AttributeList &Attr) {
4885 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004886 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004887 return;
4888
4889 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4890 S.Context,
4891 Args.data(), Args.size(),
4892 Attr.getAttributeSpellingListIndex()));
4893}
4894
4895static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4896 const AttributeList &Attr) {
4897 SmallVector<Expr*, 2> Args;
4898 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4899 return;
4900
4901 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4902 S.Context,
4903 Attr.getArgAsExpr(0),
4904 Args.data(),
4905 Args.size(),
4906 Attr.getAttributeSpellingListIndex()));
4907}
4908
4909static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4910 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004911 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004912 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004913 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004914
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004915 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4916 Attr.getRange(), S.Context, Args.data(), Args.size(),
4917 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004918}
4919
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004920static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4921 const AttributeList &Attr) {
4922 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4923 return;
4924
4925 // check that all arguments are lockable objects
4926 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004927 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004928 if (Args.empty())
4929 return;
4930
4931 RequiresCapabilityAttr *RCA = ::new (S.Context)
4932 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4933 Args.size(), Attr.getAttributeSpellingListIndex());
4934
4935 D->addAttr(RCA);
4936}
4937
Aaron Ballman43f40102014-11-14 22:34:56 +00004938static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4939 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4940 if (NSD->isAnonymousNamespace()) {
4941 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4942 // Do not want to attach the attribute to the namespace because that will
4943 // cause confusing diagnostic reports for uses of declarations within the
4944 // namespace.
4945 return;
4946 }
4947 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004948
4949 if (!S.getLangOpts().CPlusPlus14)
4950 if (Attr.isCXX11Attribute() &&
4951 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004952 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004953
Aaron Ballman43f40102014-11-14 22:34:56 +00004954 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4955}
4956
Peter Collingbourne915df992015-05-15 18:33:32 +00004957static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4958 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4959 return;
4960
4961 std::vector<std::string> Sanitizers;
4962
4963 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4964 StringRef SanitizerName;
4965 SourceLocation LiteralLoc;
4966
4967 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4968 return;
4969
4970 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4971 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4972
4973 Sanitizers.push_back(SanitizerName);
4974 }
4975
4976 D->addAttr(::new (S.Context) NoSanitizeAttr(
4977 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4978 Attr.getAttributeSpellingListIndex()));
4979}
4980
4981static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4982 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004983 StringRef AttrName = Attr.getName()->getName();
4984 normalizeName(AttrName);
Peter Collingbourne915df992015-05-15 18:33:32 +00004985 std::string SanitizerName =
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004986 llvm::StringSwitch<std::string>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00004987 .Case("no_address_safety_analysis", "address")
4988 .Case("no_sanitize_address", "address")
4989 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004990 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004991 D->addAttr(::new (S.Context)
4992 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4993 Attr.getAttributeSpellingListIndex()));
4994}
4995
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004996static void handleInternalLinkageAttr(Sema &S, Decl *D,
4997 const AttributeList &Attr) {
4998 if (InternalLinkageAttr *Internal =
4999 S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(),
5000 Attr.getAttributeSpellingListIndex()))
5001 D->addAttr(Internal);
5002}
5003
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005004/// Handles semantic checking for features that are common to all attributes,
5005/// such as checking whether a parameter was properly specified, or the correct
5006/// number of arguments were passed, etc.
5007static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
5008 const AttributeList &Attr) {
5009 // Several attributes carry different semantics than the parsing requires, so
5010 // those are opted out of the common handling.
5011 //
5012 // We also bail on unknown and ignored attributes because those are handled
5013 // as part of the target-specific handling logic.
5014 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005015 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005016 return false;
5017
Aaron Ballman3aff6332013-12-02 19:30:36 +00005018 // Check whether the attribute requires specific language extensions to be
5019 // enabled.
5020 if (!Attr.diagnoseLangOpts(S))
5021 return true;
5022
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005023 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
5024 // If there are no optional arguments, then checking for the argument count
5025 // is trivial.
5026 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
5027 return true;
5028 } else {
5029 // There are optional arguments, so checking is slightly more involved.
5030 if (Attr.getMinArgs() &&
5031 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
5032 return true;
5033 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
5034 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
5035 return true;
5036 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005037
5038 // Check whether the attribute appertains to the given subject.
5039 if (!Attr.diagnoseAppertainsTo(S, D))
5040 return true;
5041
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005042 return false;
5043}
5044
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005045//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005046// Top Level Sema Entry Points
5047//===----------------------------------------------------------------------===//
5048
Richard Smithf8a75c32013-08-29 00:47:48 +00005049/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5050/// the attribute applies to decls. If the attribute is a type attribute, just
5051/// silently ignore it if a GNU attribute.
5052static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5053 const AttributeList &Attr,
5054 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005055 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005056 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005057
Richard Smithf8a75c32013-08-29 00:47:48 +00005058 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5059 // instead.
5060 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5061 return;
5062
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005063 // Unknown attributes are automatically warned on. Target-specific attributes
5064 // which do not apply to the current target architecture are treated as
5065 // though they were unknown attributes.
5066 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00005067 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005068 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
5069 ? diag::warn_unhandled_ms_attribute_ignored
5070 : diag::warn_unknown_attribute_ignored)
5071 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005072 return;
5073 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005074
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005075 if (handleCommonAttributeFeatures(S, scope, D, Attr))
5076 return;
5077
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005078 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005079 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005080 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005081 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005082 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005083 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005084 handleInterruptAttr(S, D, Attr);
5085 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005086 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005087 handleX86ForceAlignArgPointerAttr(S, D, Attr);
5088 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005089 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005090 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00005091 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005092 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005093 case AttributeList::AT_Mips16:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005094 handleSimpleAttributeWithExclusions<Mips16Attr, MipsInterruptAttr>(S, D,
5095 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005096 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005097 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005098 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
5099 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005100 case AttributeList::AT_AMDGPUNumVGPR:
5101 handleAMDGPUNumVGPRAttr(S, D, Attr);
5102 break;
5103 case AttributeList::AT_AMDGPUNumSGPR:
5104 handleAMDGPUNumSGPRAttr(S, D, Attr);
5105 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00005106 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005107 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
5108 break;
5109 case AttributeList::AT_IBOutlet:
5110 handleIBOutlet(S, D, Attr);
5111 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005112 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005113 handleIBOutletCollection(S, D, Attr);
5114 break;
5115 case AttributeList::AT_Alias:
5116 handleAliasAttr(S, D, Attr);
5117 break;
5118 case AttributeList::AT_Aligned:
5119 handleAlignedAttr(S, D, Attr);
5120 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00005121 case AttributeList::AT_AlignValue:
5122 handleAlignValueAttr(S, D, Attr);
5123 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005124 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00005125 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005126 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005127 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005128 handleAnalyzerNoReturnAttr(S, D, Attr);
5129 break;
5130 case AttributeList::AT_TLSModel:
5131 handleTLSModelAttr(S, D, Attr);
5132 break;
5133 case AttributeList::AT_Annotate:
5134 handleAnnotateAttr(S, D, Attr);
5135 break;
5136 case AttributeList::AT_Availability:
5137 handleAvailabilityAttr(S, D, Attr);
5138 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005139 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00005140 handleDependencyAttr(S, scope, D, Attr);
5141 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005142 case AttributeList::AT_Common:
5143 handleCommonAttr(S, D, Attr);
5144 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005145 case AttributeList::AT_CUDAConstant:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005146 handleSimpleAttributeWithExclusions<CUDAConstantAttr, CUDASharedAttr>(S, D,
5147 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005148 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005149 case AttributeList::AT_PassObjectSize:
5150 handlePassObjectSizeAttr(S, D, Attr);
5151 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005152 case AttributeList::AT_Constructor:
5153 handleConstructorAttr(S, D, Attr);
5154 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005155 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005156 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
5157 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005158 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00005159 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005160 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005161 case AttributeList::AT_Destructor:
5162 handleDestructorAttr(S, D, Attr);
5163 break;
5164 case AttributeList::AT_EnableIf:
5165 handleEnableIfAttr(S, D, Attr);
5166 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005167 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005168 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005169 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00005170 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00005171 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00005172 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00005173 case AttributeList::AT_OptimizeNone:
5174 handleOptimizeNoneAttr(S, D, Attr);
5175 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00005176 case AttributeList::AT_FlagEnum:
5177 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
5178 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00005179 case AttributeList::AT_Flatten:
5180 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
5181 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005182 case AttributeList::AT_Format:
5183 handleFormatAttr(S, D, Attr);
5184 break;
5185 case AttributeList::AT_FormatArg:
5186 handleFormatArgAttr(S, D, Attr);
5187 break;
5188 case AttributeList::AT_CUDAGlobal:
5189 handleGlobalAttr(S, D, Attr);
5190 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00005191 case AttributeList::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005192 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
5193 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005194 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005195 case AttributeList::AT_CUDAHost:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005196 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D,
5197 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005198 break;
5199 case AttributeList::AT_GNUInline:
5200 handleGNUInlineAttr(S, D, Attr);
5201 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005202 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005203 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00005204 break;
David Majnemer631a90b2015-02-04 07:23:21 +00005205 case AttributeList::AT_Restrict:
5206 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005207 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005208 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005209 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
5210 break;
5211 case AttributeList::AT_Mode:
5212 handleModeAttr(S, D, Attr);
5213 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00005214 case AttributeList::AT_NoAlias:
5215 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
5216 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005217 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005218 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
5219 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00005220 case AttributeList::AT_NoSplitStack:
5221 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
5222 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00005223 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005224 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
5225 handleNonNullAttrParameter(S, PVD, Attr);
5226 else
5227 handleNonNullAttr(S, D, Attr);
5228 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00005229 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005230 handleReturnsNonNullAttr(S, D, Attr);
5231 break;
Hal Finkelee90a222014-09-26 05:04:30 +00005232 case AttributeList::AT_AssumeAligned:
5233 handleAssumeAlignedAttr(S, D, Attr);
5234 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005235 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005236 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
5237 break;
5238 case AttributeList::AT_Ownership:
5239 handleOwnershipAttr(S, D, Attr);
5240 break;
5241 case AttributeList::AT_Cold:
5242 handleColdAttr(S, D, Attr);
5243 break;
5244 case AttributeList::AT_Hot:
5245 handleHotAttr(S, D, Attr);
5246 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005247 case AttributeList::AT_Naked:
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005248 handleNakedAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005249 break;
5250 case AttributeList::AT_NoReturn:
5251 handleNoReturnAttr(S, D, Attr);
5252 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005253 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005254 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
5255 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005256 case AttributeList::AT_CUDAShared:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005257 handleSimpleAttributeWithExclusions<CUDASharedAttr, CUDAConstantAttr>(S, D,
5258 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005259 break;
5260 case AttributeList::AT_VecReturn:
5261 handleVecReturnAttr(S, D, Attr);
5262 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005263
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005264 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005265 handleObjCOwnershipAttr(S, D, Attr);
5266 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005267 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005268 handleObjCPreciseLifetimeAttr(S, D, Attr);
5269 break;
John McCall31168b02011-06-15 23:02:42 +00005270
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005271 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005272 handleObjCReturnsInnerPointerAttr(S, D, Attr);
5273 break;
John McCallcf166702011-07-22 08:53:00 +00005274
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005275 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005276 handleObjCRequiresSuperAttr(S, D, Attr);
5277 break;
5278
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005279 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005280 handleObjCBridgeAttr(S, scope, D, Attr);
5281 break;
5282
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005283 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005284 handleObjCBridgeMutableAttr(S, scope, D, Attr);
5285 break;
5286
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005287 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005288 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
5289 break;
John McCallf1e8b342011-09-29 07:17:38 +00005290
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005291 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005292 handleObjCDesignatedInitializer(S, D, Attr);
5293 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005294
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005295 case AttributeList::AT_ObjCRuntimeName:
5296 handleObjCRuntimeName(S, D, Attr);
5297 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005298
5299 case AttributeList::AT_ObjCBoxable:
5300 handleObjCBoxable(S, D, Attr);
5301 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005302
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005303 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005304 handleCFAuditedTransferAttr(S, D, Attr);
5305 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005306 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005307 handleCFUnknownTransferAttr(S, D, Attr);
5308 break;
John McCall32f5fe12011-09-30 05:12:12 +00005309
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005310 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005311 case AttributeList::AT_NSConsumed:
5312 handleNSConsumedAttr(S, D, Attr);
5313 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005314 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005315 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
5316 break;
John McCalled433932011-01-25 03:31:58 +00005317
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005318 case AttributeList::AT_NSReturnsAutoreleased:
5319 case AttributeList::AT_NSReturnsNotRetained:
5320 case AttributeList::AT_CFReturnsNotRetained:
5321 case AttributeList::AT_NSReturnsRetained:
5322 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005323 handleNSReturnsRetainedAttr(S, D, Attr);
5324 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00005325 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005326 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
5327 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005328 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005329 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
5330 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005331 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005332 handleVecTypeHint(S, D, Attr);
5333 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005334
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005335 case AttributeList::AT_InitPriority:
5336 handleInitPriorityAttr(S, D, Attr);
5337 break;
5338
5339 case AttributeList::AT_Packed:
5340 handlePackedAttr(S, D, Attr);
5341 break;
5342 case AttributeList::AT_Section:
5343 handleSectionAttr(S, D, Attr);
5344 break;
Eric Christopher11acf732015-06-12 01:35:52 +00005345 case AttributeList::AT_Target:
5346 handleTargetAttr(S, D, Attr);
5347 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005348 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00005349 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005350 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005351 case AttributeList::AT_ArcWeakrefUnavailable:
5352 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
5353 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005354 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005355 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
5356 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00005357 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00005358 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00005359 break;
5360 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005361 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
5362 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005363 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005364 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
5365 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005366 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005367 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5368 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005369 case AttributeList::AT_NotTailCalled:
5370 handleNotTailCalledAttr(S, D, Attr);
5371 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005372 case AttributeList::AT_DisableTailCalls:
5373 handleDisableTailCallsAttr(S, D, Attr);
5374 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005375 case AttributeList::AT_Used:
5376 handleUsedAttr(S, D, Attr);
5377 break;
John McCalld041a9b2013-02-20 01:54:26 +00005378 case AttributeList::AT_Visibility:
5379 handleVisibilityAttr(S, D, Attr, false);
5380 break;
5381 case AttributeList::AT_TypeVisibility:
5382 handleVisibilityAttr(S, D, Attr, true);
5383 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005384 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005385 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5386 break;
5387 case AttributeList::AT_WarnUnusedResult:
5388 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005389 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005390 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005391 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5392 break;
5393 case AttributeList::AT_WeakRef:
5394 handleWeakRefAttr(S, D, Attr);
5395 break;
5396 case AttributeList::AT_WeakImport:
5397 handleWeakImportAttr(S, D, Attr);
5398 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005399 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005400 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005401 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005402 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005403 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5404 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005405 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005406 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005407 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005408 case AttributeList::AT_ObjCNSObject:
5409 handleObjCNSObject(S, D, Attr);
5410 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005411 case AttributeList::AT_ObjCIndependentClass:
5412 handleObjCIndependentClass(S, D, Attr);
5413 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005414 case AttributeList::AT_Blocks:
5415 handleBlocksAttr(S, D, Attr);
5416 break;
5417 case AttributeList::AT_Sentinel:
5418 handleSentinelAttr(S, D, Attr);
5419 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005420 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005421 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5422 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005423 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005424 handleSimpleAttribute<PureAttr>(S, D, Attr);
5425 break;
5426 case AttributeList::AT_Cleanup:
5427 handleCleanupAttr(S, D, Attr);
5428 break;
5429 case AttributeList::AT_NoDebug:
5430 handleNoDebugAttr(S, D, Attr);
5431 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005432 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005433 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5434 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005435 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005436 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5437 break;
5438 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5439 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5440 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005441 case AttributeList::AT_StdCall:
5442 case AttributeList::AT_CDecl:
5443 case AttributeList::AT_FastCall:
5444 case AttributeList::AT_ThisCall:
5445 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005446 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005447 case AttributeList::AT_MSABI:
5448 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005449 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005450 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005451 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005452 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005453 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005454 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5455 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00005456 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005457 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
5458 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005459 case AttributeList::AT_InternalLinkage:
5460 handleInternalLinkageAttr(S, D, Attr);
5461 break;
John McCall8d32c052012-05-22 21:28:12 +00005462
5463 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00005464 case AttributeList::AT_MSNoVTable:
5465 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005466 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005467 case AttributeList::AT_MSStruct:
5468 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005469 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005470 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005471 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005472 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005473 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005474 handleMSInheritanceAttr(S, D, Attr);
5475 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005476 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005477 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5478 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005479 case AttributeList::AT_Thread:
5480 handleDeclspecThreadAttr(S, D, Attr);
5481 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005482
5483 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005484 case AttributeList::AT_AssertExclusiveLock:
5485 handleAssertExclusiveLockAttr(S, D, Attr);
5486 break;
5487 case AttributeList::AT_AssertSharedLock:
5488 handleAssertSharedLockAttr(S, D, Attr);
5489 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005490 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005491 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5492 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005493 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005494 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005495 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005496 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005497 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5498 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005499 case AttributeList::AT_NoSanitize:
5500 handleNoSanitizeAttr(S, D, Attr);
5501 break;
5502 case AttributeList::AT_NoSanitizeSpecific:
5503 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005504 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005505 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005506 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005507 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005508 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005509 handleGuardedByAttr(S, D, Attr);
5510 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005511 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005512 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005513 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005514 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005515 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005516 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005517 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005518 handleLockReturnedAttr(S, D, Attr);
5519 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005520 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005521 handleLocksExcludedAttr(S, D, Attr);
5522 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005523 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005524 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005525 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005526 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005527 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005528 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005529 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005530 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005531 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005532
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005533 // Capability analysis attributes.
5534 case AttributeList::AT_Capability:
5535 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005536 handleCapabilityAttr(S, D, Attr);
5537 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005538 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005539 handleRequiresCapabilityAttr(S, D, Attr);
5540 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005541
5542 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005543 handleAssertCapabilityAttr(S, D, Attr);
5544 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005545 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005546 handleAcquireCapabilityAttr(S, D, Attr);
5547 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005548 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005549 handleReleaseCapabilityAttr(S, D, Attr);
5550 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005551 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005552 handleTryAcquireCapabilityAttr(S, D, Attr);
5553 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005554
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005555 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005556 case AttributeList::AT_Consumable:
5557 handleConsumableAttr(S, D, Attr);
5558 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005559 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005560 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5561 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005562 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005563 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5564 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005565 case AttributeList::AT_CallableWhen:
5566 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005567 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005568 case AttributeList::AT_ParamTypestate:
5569 handleParamTypestateAttr(S, D, Attr);
5570 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005571 case AttributeList::AT_ReturnTypestate:
5572 handleReturnTypestateAttr(S, D, Attr);
5573 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005574 case AttributeList::AT_SetTypestate:
5575 handleSetTypestateAttr(S, D, Attr);
5576 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005577 case AttributeList::AT_TestTypestate:
5578 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005579 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005580
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005581 // Type safety attributes.
5582 case AttributeList::AT_ArgumentWithTypeTag:
5583 handleArgumentWithTypeTagAttr(S, D, Attr);
5584 break;
5585 case AttributeList::AT_TypeTagForDatatype:
5586 handleTypeTagForDatatypeAttr(S, D, Attr);
5587 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005588 }
5589}
5590
5591/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5592/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005593void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005594 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005595 bool IncludeCXX11Attributes) {
5596 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005597 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005598
Joey Gouly2cd9db12013-12-13 16:15:28 +00005599 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005600 // GCC accepts
5601 // static int a9 __attribute__((weakref));
5602 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005603 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005604 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5605 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005606 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005607 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005608 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005609
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005610 // FIXME: We should be able to handle this in TableGen as well. It would be
5611 // good to have a way to specify "these attributes must appear as a group",
5612 // for these. Additionally, it would be good to have a way to specify "these
5613 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005614 if (!D->hasAttr<OpenCLKernelAttr>()) {
5615 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005616 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005617 // FIXME: This emits a different error message than
5618 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005619 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005620 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005621 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005622 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005623 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005624 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005625 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005626 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005627 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5628 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5629 << A << ExpectedKernelFunction;
5630 D->setInvalidDecl();
5631 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5632 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5633 << A << ExpectedKernelFunction;
5634 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005635 }
5636 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005637}
5638
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005639// Annotation attributes are the only attributes allowed after an access
5640// specifier.
5641bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5642 const AttributeList *AttrList) {
5643 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005644 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005645 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005646 } else {
5647 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5648 return true;
5649 }
5650 }
5651
5652 return false;
5653}
5654
John McCall42856de2011-10-01 05:17:03 +00005655/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5656/// contains any decl attributes that we should warn about.
5657static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5658 for ( ; A; A = A->getNext()) {
5659 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005660 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005661 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5662
5663 if (A->getKind() == AttributeList::UnknownAttribute) {
5664 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5665 << A->getName() << A->getRange();
5666 } else {
5667 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5668 << A->getName() << A->getRange();
5669 }
5670 }
5671}
5672
5673/// checkUnusedDeclAttributes - Given a declarator which is not being
5674/// used to build a declaration, complain about any decl attributes
5675/// which might be lying around on it.
5676void Sema::checkUnusedDeclAttributes(Declarator &D) {
5677 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5678 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5679 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5680 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5681}
5682
Ryan Flynn7d470f32009-07-30 03:15:39 +00005683/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005684/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005685NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5686 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005687 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005688 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005689 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00005690 FunctionDecl *NewFD;
5691 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00005692 // FIXME: Mangling?
5693 // FIXME: Is the qualifier info correct?
5694 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00005695 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5696 Loc, Loc, DeclarationName(II),
5697 FD->getType(), FD->getTypeSourceInfo(),
5698 SC_None, false/*isInlineSpecified*/,
5699 FD->hasPrototype(),
5700 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005701 NewD = NewFD;
5702
5703 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005704 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005705
5706 // Fake up parameter variables; they are declared as if this were
5707 // a typedef.
5708 QualType FDTy = FD->getType();
5709 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5710 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005711 for (const auto &AI : FT->param_types()) {
5712 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005713 Param->setScopeInfo(0, Params.size());
5714 Params.push_back(Param);
5715 }
David Blaikie9c70e042011-09-21 18:16:56 +00005716 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005717 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005718 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5719 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005720 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005721 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005722 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005723 if (VD->getQualifier()) {
5724 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005725 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005726 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005727 }
5728 return NewD;
5729}
5730
James Dennett634962f2012-06-14 21:40:34 +00005731/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005732/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005733void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005734 if (W.getUsed()) return; // only do this once
5735 W.setUsed(true);
5736 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5737 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005738 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005739 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5740 W.getLocation()));
5741 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005742 WeakTopLevelDecl.push_back(NewD);
5743 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5744 // to insert Decl at TU scope, sorry.
5745 DeclContext *SavedContext = CurContext;
5746 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005747 NewD->setDeclContext(CurContext);
5748 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005749 PushOnScopeChains(NewD, S);
5750 CurContext = SavedContext;
5751 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005752 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005753 }
5754}
5755
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005756void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5757 // It's valid to "forward-declare" #pragma weak, in which case we
5758 // have to do this.
5759 LoadExternalWeakUndeclaredIdentifiers();
5760 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005761 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005762 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5763 if (VD->isExternC())
5764 ND = VD;
5765 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5766 if (FD->isExternC())
5767 ND = FD;
5768 if (ND) {
5769 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005770 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005771 if (I != WeakUndeclaredIdentifiers.end()) {
5772 WeakInfo W = I->second;
5773 DeclApplyPragmaWeak(S, ND, W);
5774 WeakUndeclaredIdentifiers[Id] = W;
5775 }
5776 }
5777 }
5778 }
5779}
5780
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005781/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5782/// it, apply them to D. This is a bit tricky because PD can have attributes
5783/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005784void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005785 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005786 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005787 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005788
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005789 // Walk the declarator structure, applying decl attributes that were in a type
5790 // position to the decl itself. This handles cases like:
5791 // int *__attr__(x)** D;
5792 // when X is a decl attribute.
5793 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5794 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005795 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005796
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005797 // Finally, apply any attributes on the decl itself.
5798 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005799 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005800}
John McCall28a6aea2009-11-04 02:18:39 +00005801
John McCall31168b02011-06-15 23:02:42 +00005802/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00005803/// If so, it'll still be annotated with an attribute that makes it
5804/// illegal to actually use.
5805static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
5806 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00005807 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00005808 // Private ivars are always okay. Unfortunately, people don't
5809 // always properly make their ivars private, even in system headers.
5810 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005811 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5812 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005813 return false;
5814
John McCallc6af8c62015-10-28 05:03:19 +00005815 // Silently accept unsupported uses of __weak in both user and system
5816 // declarations when it's been disabled, for ease of integration with
5817 // -fno-objc-arc files. We do have to take some care against attempts
5818 // to define such things; for now, we've only done that for ivars
5819 // and properties.
5820 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
5821 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
5822 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
5823 reason = UnavailableAttr::IR_ForbiddenWeak;
5824 return true;
5825 }
John McCallb61e14e2015-10-27 04:54:50 +00005826 }
5827
John McCallc6af8c62015-10-28 05:03:19 +00005828 // Allow all sorts of things in system headers.
5829 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
5830 // Currently, all the failures dealt with this way are due to ARC
5831 // restrictions.
5832 reason = UnavailableAttr::IR_ARCForbiddenType;
5833 return true;
John McCallb61e14e2015-10-27 04:54:50 +00005834 }
5835
5836 return false;
John McCall31168b02011-06-15 23:02:42 +00005837}
5838
5839/// Handle a delayed forbidden-type diagnostic.
5840static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5841 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00005842 auto reason = UnavailableAttr::IR_None;
5843 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
5844 assert(reason && "didn't set reason?");
5845 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
5846 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005847 return;
5848 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005849 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005850 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005851 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005852 // kind of forbidden type messages on unavailable functions.
5853 if (FD->hasAttr<UnavailableAttr>() &&
5854 diag.getForbiddenTypeDiagnostic() ==
5855 diag::err_arc_array_param_no_ownership) {
5856 diag.Triggered = true;
5857 return;
5858 }
5859 }
John McCall31168b02011-06-15 23:02:42 +00005860
5861 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5862 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5863 diag.Triggered = true;
5864}
5865
Aaron Ballmanfb237522014-10-15 15:37:51 +00005866
5867static bool isDeclDeprecated(Decl *D) {
5868 do {
5869 if (D->isDeprecated())
5870 return true;
5871 // A category implicitly has the availability of the interface.
5872 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005873 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5874 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005875 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5876 return false;
5877}
5878
5879static bool isDeclUnavailable(Decl *D) {
5880 do {
5881 if (D->isUnavailable())
5882 return true;
5883 // A category implicitly has the availability of the interface.
5884 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005885 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5886 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005887 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5888 return false;
5889}
5890
Nico Weber0055a192015-03-19 19:18:22 +00005891static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005892 Decl *Ctx, const NamedDecl *D,
5893 StringRef Message, SourceLocation Loc,
5894 const ObjCInterfaceDecl *UnknownObjCClass,
5895 const ObjCPropertyDecl *ObjCProperty,
5896 bool ObjCPropertyAccess) {
5897 // Diagnostics for deprecated or unavailable.
5898 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00005899 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005900
5901 // Matches 'diag::note_property_attribute' options.
5902 unsigned property_note_select;
5903
5904 // Matches diag::note_availability_specified_here.
5905 unsigned available_here_select_kind;
5906
5907 // Don't warn if our current context is deprecated or unavailable.
5908 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005909 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005910 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005911 return;
5912 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5913 : diag::warn_property_method_deprecated;
5914 diag_message = diag::warn_deprecated_message;
5915 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5916 property_note_select = /* deprecated */ 0;
5917 available_here_select_kind = /* deprecated */ 2;
5918 break;
5919
Nico Weber0055a192015-03-19 19:18:22 +00005920 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005921 if (isDeclUnavailable(Ctx))
5922 return;
5923 diag = !ObjCPropertyAccess ? diag::err_unavailable
5924 : diag::err_property_method_unavailable;
5925 diag_message = diag::err_unavailable_message;
5926 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5927 property_note_select = /* unavailable */ 1;
5928 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00005929
John McCallc6af8c62015-10-28 05:03:19 +00005930 if (auto attr = D->getAttr<UnavailableAttr>()) {
5931 if (attr->isImplicit() && attr->getImplicitReason()) {
5932 // Most of these failures are due to extra restrictions in ARC;
5933 // reflect that in the primary diagnostic when applicable.
5934 auto flagARCError = [&] {
5935 if (S.getLangOpts().ObjCAutoRefCount &&
5936 S.getSourceManager().isInSystemHeader(D->getLocation()))
5937 diag = diag::err_unavailable_in_arc;
5938 };
5939
5940 switch (attr->getImplicitReason()) {
5941 case UnavailableAttr::IR_None: break;
5942
5943 case UnavailableAttr::IR_ARCForbiddenType:
5944 flagARCError();
5945 diag_available_here = diag::note_arc_forbidden_type;
5946 break;
5947
5948 case UnavailableAttr::IR_ForbiddenWeak:
5949 if (S.getLangOpts().ObjCWeakRuntime)
5950 diag_available_here = diag::note_arc_weak_disabled;
5951 else
5952 diag_available_here = diag::note_arc_weak_no_runtime;
5953 break;
5954
5955 case UnavailableAttr::IR_ARCForbiddenConversion:
5956 flagARCError();
5957 diag_available_here = diag::note_performs_forbidden_arc_conversion;
5958 break;
5959
5960 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
5961 flagARCError();
5962 diag_available_here = diag::note_arc_init_returns_unrelated;
5963 break;
5964
5965 case UnavailableAttr::IR_ARCFieldWithOwnership:
5966 flagARCError();
5967 diag_available_here = diag::note_arc_field_with_ownership;
5968 break;
5969 }
5970 }
John McCallb61e14e2015-10-27 04:54:50 +00005971 }
5972
Aaron Ballmanfb237522014-10-15 15:37:51 +00005973 break;
5974
Nico Weber0055a192015-03-19 19:18:22 +00005975 case Sema::AD_Partial:
5976 diag = diag::warn_partial_availability;
5977 diag_message = diag::warn_partial_message;
5978 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5979 property_note_select = /* partial */ 2;
5980 available_here_select_kind = /* partial */ 3;
5981 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005982 }
5983
Aaron Ballmanfb237522014-10-15 15:37:51 +00005984 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005985 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005986 if (ObjCProperty)
5987 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5988 << ObjCProperty->getDeclName() << property_note_select;
5989 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005990 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005991 if (ObjCProperty)
5992 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5993 << ObjCProperty->getDeclName() << property_note_select;
5994 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005995 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005996 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5997 }
5998
John McCallb61e14e2015-10-27 04:54:50 +00005999 S.Diag(D->getLocation(), diag_available_here)
Aaron Ballmanfb237522014-10-15 15:37:51 +00006000 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00006001 if (K == Sema::AD_Partial)
6002 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00006003}
6004
6005static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
6006 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00006007 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
6008 DD.Kind == DelayedDiagnostic::Unavailable);
6009 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
6010 ? Sema::AD_Deprecation
6011 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00006012 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00006013 DoEmitAvailabilityWarning(
6014 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
6015 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00006016}
6017
John McCall2ec85372012-05-07 06:16:41 +00006018void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
6019 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00006020 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00006021 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00006022
John McCall2ec85372012-05-07 06:16:41 +00006023 // When delaying diagnostics to run in the context of a parsed
6024 // declaration, we only want to actually emit anything if parsing
6025 // succeeds.
6026 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00006027
John McCall2ec85372012-05-07 06:16:41 +00006028 // We emit all the active diagnostics in this pool or any of its
6029 // parents. In general, we'll get one pool for the decl spec
6030 // and a child pool for each declarator; in a decl group like:
6031 // deprecated_typedef foo, *bar, baz();
6032 // only the declarator pops will be passed decls. This is correct;
6033 // we really do need to consider delayed diagnostics from the decl spec
6034 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00006035 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00006036 do {
John McCall6347b682012-05-07 06:16:58 +00006037 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00006038 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
6039 // This const_cast is a bit lame. Really, Triggered should be mutable.
6040 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00006041 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00006042 continue;
6043
John McCallc1465822011-02-14 07:13:47 +00006044 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00006045 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00006046 case DelayedDiagnostic::Unavailable:
6047 // Don't bother giving deprecation/unavailable diagnostics if
6048 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00006049 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00006050 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00006051 break;
6052
6053 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00006054 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00006055 break;
John McCall31168b02011-06-15 23:02:42 +00006056
6057 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00006058 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00006059 break;
John McCall86121512010-01-27 03:50:35 +00006060 }
6061 }
John McCall2ec85372012-05-07 06:16:41 +00006062 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00006063}
6064
John McCall6347b682012-05-07 06:16:58 +00006065/// Given a set of delayed diagnostics, re-emit them as if they had
6066/// been delayed in the current context instead of in the given pool.
6067/// Essentially, this just moves them to the current pool.
6068void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
6069 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
6070 assert(curPool && "re-emitting in undelayed context not supported");
6071 curPool->steal(pool);
6072}
6073
Ted Kremenekb79ee572013-12-18 23:30:06 +00006074void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
6075 NamedDecl *D, StringRef Message,
6076 SourceLocation Loc,
6077 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00006078 const ObjCPropertyDecl *ObjCProperty,
6079 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00006080 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00006081 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00006082 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
6083 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
6084 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00006085 return;
6086 }
6087
Ted Kremenekb79ee572013-12-18 23:30:06 +00006088 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00006089 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
6090 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00006091}