36#include "llvm/ADT/ArrayRef.h"
37#include "llvm/ADT/DenseMap.h"
38#include "llvm/ADT/STLExtras.h"
39#include "llvm/ADT/SmallPtrSet.h"
40#include "llvm/ADT/SmallString.h"
41#include "llvm/ADT/SmallVector.h"
69 bool HasLeadingEmptyMacro) {
98 Diag(
decl->getLocation(), diag::err_non_variable_decl_in_for);
99 decl->setInvalidDecl();
132 enum { Equality, Inequality, Relational, ThreeWay } Kind;
135 if (!Op->isComparisonOp())
138 if (Op->getOpcode() == BO_EQ)
140 else if (Op->getOpcode() == BO_NE)
142 else if (Op->getOpcode() == BO_Cmp)
145 assert(Op->isRelationalOp());
148 Loc = Op->getOperatorLoc();
149 CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue();
151 switch (Op->getOperator()) {
155 case OO_ExclaimEqual:
160 case OO_GreaterEqual:
171 Loc = Op->getOperatorLoc();
172 CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue();
183 S.
Diag(Loc, diag::warn_unused_comparison)
189 if (Kind == Inequality)
190 S.
Diag(Loc, diag::note_inequality_comparison_to_or_assign)
192 else if (Kind == Equality)
193 S.
Diag(Loc, diag::note_equality_comparison_to_assign)
205 StringRef Msg = A->getMessage();
209 return S.
Diag(Loc, diag::warn_unused_constructor) << A << R1 << R2;
210 return S.
Diag(Loc, diag::warn_unused_result) << A << R1 << R2;
214 return S.
Diag(Loc, diag::warn_unused_constructor_msg) << A << Msg << R1
216 return S.
Diag(Loc, diag::warn_unused_result_msg) << A << Msg << R1 << R2;
223 const Expr *E = dyn_cast_or_null<Expr>(S);
238 bool ShouldSuppress =
242 const Expr *WarnExpr;
267 if (
const FullExpr *Temps = dyn_cast<FullExpr>(E))
268 E = Temps->getSubExpr();
270 E = TempExpr->getSubExpr();
276 if (
const auto *Cast = dyn_cast<CastExpr>(E))
277 if (Cast->getCastKind() == CK_NoOp ||
278 Cast->getCastKind() == CK_ConstructorConversion)
279 E = Cast->getSubExpr()->IgnoreImpCasts();
281 if (
const CallExpr *CE = dyn_cast<CallExpr>(E)) {
286 CE->getUnusedResultAttr(
Context)),
294 if (
const Decl *FD = CE->getCalleeDecl()) {
297 if (FD->hasAttr<PureAttr>()) {
298 Diag(Loc, diag::warn_unused_call) << R1 << R2 <<
"pure";
301 if (FD->hasAttr<ConstAttr>()) {
302 Diag(Loc, diag::warn_unused_call) << R1 << R2 <<
"const";
306 }
else if (
const auto *CE = dyn_cast<CXXConstructExpr>(E)) {
308 const auto *A = Ctor->getAttr<WarnUnusedResultAttr>();
309 A = A ? A : Ctor->getParent()->getAttr<WarnUnusedResultAttr>();
313 }
else if (
const auto *ILE = dyn_cast<InitListExpr>(E)) {
314 if (
const TagDecl *TD = ILE->getType()->getAsTagDecl()) {
320 }
else if (ShouldSuppress)
325 if (
getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) {
326 Diag(Loc, diag::err_arc_unused_init_message) << R1;
336 const Expr *Source = POE->getSyntacticForm();
338 if (
LangOpts.OpenMP && isa<CallExpr>(Source) &&
339 POE->getNumSemanticExprs() == 1 &&
340 isa<CallExpr>(POE->getSemanticExpr(0)))
342 if (isa<ObjCSubscriptRefExpr>(Source))
343 DiagID = diag::warn_unused_container_subscript_expr;
344 else if (isa<ObjCPropertyRefExpr>(Source))
345 DiagID = diag::warn_unused_property_expr;
347 = dyn_cast<CXXFunctionalCastExpr>(E)) {
348 const Expr *E = FC->getSubExpr();
350 E = TE->getSubExpr();
351 if (isa<CXXTemporaryObjectExpr>(E))
354 if (
const CXXRecordDecl *RD = CE->getType()->getAsCXXRecordDecl())
355 if (!RD->getAttr<WarnUnusedAttr>())
359 else if (
const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {
367 Diag(Loc, diag::warn_unused_voidptr)
377 Diag(Loc, diag::warn_unused_volatile) << R1 << R2;
384 if (DiagID != diag::warn_unused_comma_left_operand || !
isSFINAEContext())
386 PDiag(DiagID) << R1 << R2);
411 const unsigned NumElts = Elts.size();
416 const unsigned MixedDeclsCodeID =
getLangOpts().C99
417 ? diag::warn_mixed_decls_code
418 : diag::ext_mixed_decls_code;
423 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
427 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
431 Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
441 for (
unsigned i = 0; i != NumElts - 1; ++i)
477 auto CheckAndFinish = [&](
Expr *E) {
484 llvm::APSInt TempVal;
490 if (!E->isValueDependent())
504 if (Converted.
get() == Val.
get())
505 Converted = CheckAndFinish(Val.
get());
513 assert((LHSVal.
isInvalid() || LHSVal.
get()) &&
"missing LHS value");
516 "missing RHS value");
519 Diag(CaseLoc, diag::err_case_not_in_switch);
529 CaseLoc, DotDotDotLoc, ColonLoc);
536 cast<CaseStmt>(S)->setSubStmt(SubStmt);
543 Diag(DefaultLoc, diag::err_default_not_in_switch);
557 Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->
getDeclName();
565 Diag(IdentLoc, diag::warn_reserved_extern_symbol)
566 << TheDecl << static_cast<int>(Status);
587 for (
const auto *A : Attrs) {
588 if (A->getKind() == attr::MustTail) {
603 if (!SemanticAttrs.empty())
619 if (!checkMustTailAttr(St, MTA))
625 auto IgnoreImplicitAsWritten = [](
Expr *E) ->
Expr * {
636bool Sema::checkMustTailAttr(
const Stmt *St,
const Attr &MTA) {
638 "musttail cannot be checked from a dependent context");
641 auto IgnoreParenImplicitAsWritten = [](
const Expr *E) ->
const Expr * {
647 const Expr *E = cast<ReturnStmt>(St)->getRetValue();
648 const auto *CE = dyn_cast_or_null<CallExpr>(IgnoreParenImplicitAsWritten(E));
655 if (
const auto *EWC = dyn_cast<ExprWithCleanups>(E)) {
656 if (EWC->cleanupsHaveSideEffects()) {
657 Diag(St->
getBeginLoc(), diag::err_musttail_needs_trivial_args) << &MTA;
668 ft_non_static_member,
669 ft_pointer_to_member,
670 } MemberType = ft_non_member;
675 } CallerType, CalleeType;
678 bool IsCallee) ->
bool {
679 if (isa<CXXConstructorDecl, CXXDestructorDecl>(CMD)) {
681 << IsCallee << isa<CXXDestructorDecl>(CMD);
683 Diag(CMD->getBeginLoc(), diag::note_musttail_structors_forbidden)
684 << isa<CXXDestructorDecl>(CMD);
689 Type.MemberType = FuncType::ft_static_member;
692 Type.MemberType = FuncType::ft_non_static_member;
698 const auto *CallerDecl = dyn_cast<FunctionDecl>(
CurContext);
709 Diag(St->
getBeginLoc(), diag::err_musttail_forbidden_from_this_context)
712 }
else if (
const auto *CMD = dyn_cast<CXXMethodDecl>(
CurContext)) {
714 if (!GetMethodType(CMD, CallerType,
false))
722 const auto *CalleeBinOp = dyn_cast<BinaryOperator>(CalleeExpr);
724 ? CE->getCalleeDecl()->getBeginLoc()
729 dyn_cast_or_null<CXXMethodDecl>(CE->getCalleeDecl())) {
731 if (!GetMethodType(CMD, CalleeType,
true))
733 }
else if (CalleeBinOp && CalleeBinOp->isPtrMemOp()) {
737 CalleeType.This =
QualType(MPT->getClass(), 0);
739 CalleeType.MemberType = FuncType::ft_pointer_to_member;
740 }
else if (isa<CXXPseudoDestructorExpr>(CalleeExpr)) {
752 if (!CalleeType.Func || !CallerType.Func) {
754 if (!CalleeType.Func && CE->getDirectCallee()) {
755 Diag(CE->getDirectCallee()->getBeginLoc(),
756 diag::note_musttail_fix_non_prototype);
758 if (!CallerType.Func)
759 Diag(CallerDecl->getBeginLoc(), diag::note_musttail_fix_non_prototype);
770 if (CallerType.Func->getCallConv() != CalleeType.Func->getCallConv()) {
771 if (
const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl()))
773 <<
true << ND->getDeclName();
775 Diag(St->
getBeginLoc(), diag::err_musttail_callconv_mismatch) <<
false;
776 Diag(CalleeLoc, diag::note_musttail_callconv_mismatch)
783 if (CalleeType.Func->isVariadic() || CallerType.Func->isVariadic()) {
789 if (CallerType.This.isNull() != CalleeType.This.isNull()) {
790 if (
const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
792 << CallerType.MemberType << CalleeType.MemberType <<
true
793 << ND->getDeclName();
794 Diag(CalleeLoc, diag::note_musttail_callee_defined_here)
795 << ND->getDeclName();
798 << CallerType.MemberType << CalleeType.MemberType <<
false;
803 auto CheckTypesMatch = [
this](FuncType CallerType, FuncType CalleeType,
813 unsigned Select) ->
bool {
821 if (!CallerType.This.isNull() &&
825 if (!DoTypesMatch(CallerType.Func->getReturnType(),
829 if (CallerType.Func->getNumParams() != CalleeType.Func->getNumParams()) {
831 << CalleeType.Func->getNumParams();
837 size_t N = CallerType.Func->getNumParams();
838 for (
size_t I = 0; I < N; I++) {
839 if (!DoTypesMatch(CalleeParams[I], CallerParams[I],
841 PD << static_cast<int>(I) + 1;
850 if (!CheckTypesMatch(CallerType, CalleeType, PD)) {
851 if (
const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl()))
853 <<
true << ND->getDeclName();
869 CommaVisitor(
Sema &SemaRef) : Inherited(SemaRef.Context), SemaRef(SemaRef) {}
887 bool ConstevalOrNegatedConsteval =
891 Expr *CondExpr = Cond.
get().second;
892 assert((CondExpr || ConstevalOrNegatedConsteval) &&
893 "If statement: missing condition");
897 CommaVisitor(*this).Visit(CondExpr);
899 if (!ConstevalOrNegatedConsteval && !elseStmt)
902 if (ConstevalOrNegatedConsteval ||
904 auto DiagnoseLikelihood = [&](
const Stmt *S) {
907 diag::warn_attribute_has_no_effect_on_compile_time_if)
908 << A << ConstevalOrNegatedConsteval << A->getRange();
910 diag::note_attribute_has_no_effect_on_compile_time_if_here)
911 << ConstevalOrNegatedConsteval
918 DiagnoseLikelihood(thenStmt);
919 DiagnoseLikelihood(elseStmt);
921 std::tuple<bool, const Attr *, const Attr *> LHC =
923 if (std::get<0>(LHC)) {
924 const Attr *ThenAttr = std::get<1>(LHC);
925 const Attr *ElseAttr = std::get<2>(LHC);
927 diag::warn_attributes_likelihood_ifstmt_conflict)
928 << ThenAttr << ThenAttr->
getRange();
930 << ElseAttr << ElseAttr->
getRange();
934 if (ConstevalOrNegatedConsteval) {
939 if (FD && FD->isConsteval())
943 Diags.
Report(IfLoc, diag::warn_consteval_if_always_true) << Immediate;
946 return BuildIfStmt(IfLoc, StatementKind, LParenLoc, InitStmt, Cond, RParenLoc,
947 thenStmt, ElseLoc, elseStmt);
960 isa<ObjCAvailabilityCheckExpr>(Cond.
get().second))
964 Cond.
get().first, Cond.
get().second, LParenLoc,
965 RParenLoc, thenStmt, ElseLoc, elseStmt);
969 struct CaseCompareFunctor {
970 bool operator()(
const std::pair<llvm::APSInt, CaseStmt*> &LHS,
971 const llvm::APSInt &RHS) {
972 return LHS.first < RHS;
974 bool operator()(
const std::pair<llvm::APSInt, CaseStmt*> &LHS,
975 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
976 return LHS.first < RHS.first;
978 bool operator()(
const llvm::APSInt &LHS,
979 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
980 return LHS < RHS.first;
987static bool CmpCaseVals(
const std::pair<llvm::APSInt, CaseStmt*>& lhs,
988 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
989 if (lhs.first < rhs.first)
992 if (lhs.first == rhs.first &&
993 lhs.second->getCaseLoc() < rhs.second->getCaseLoc())
1000static bool CmpEnumVals(
const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
1001 const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
1003 return lhs.first < rhs.first;
1008static bool EqEnumVals(
const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
1009 const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
1011 return lhs.first == rhs.first;
1017 if (
const auto *FE = dyn_cast<FullExpr>(E))
1018 E = FE->getSubExpr();
1019 while (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
1020 if (ImpCast->getCastKind() != CK_IntegralCast)
break;
1021 E = ImpCast->getSubExpr();
1031 SwitchConvertDiagnoser(
Expr *Cond)
1037 return S.
Diag(Loc, diag::err_typecheck_statement_requires_integer) << T;
1042 return S.
Diag(Loc, diag::err_switch_incomplete_class_type)
1048 return S.
Diag(Loc, diag::err_switch_explicit_conversion) << T << ConvTy;
1059 return S.
Diag(Loc, diag::err_switch_multiple_conversions) << T;
1070 llvm_unreachable(
"conversion functions are permitted");
1072 } SwitchDiagnoser(Cond);
1081 Cond = CondResult.
get();
1094 Expr *CondExpr = Cond.
get().second;
1095 assert((Cond.
isInvalid() || CondExpr) &&
"switch with no condition");
1108 Diag(SwitchLoc, diag::warn_bool_switch_condition)
1116 LParenLoc, RParenLoc);
1122static void AdjustAPSInt(llvm::APSInt &Val,
unsigned BitWidth,
bool IsSigned) {
1123 Val = Val.extOrTrunc(BitWidth);
1124 Val.setIsSigned(IsSigned);
1130 unsigned UnpromotedWidth,
bool UnpromotedSign) {
1138 if (UnpromotedWidth < Val.getBitWidth()) {
1139 llvm::APSInt ConvVal(Val);
1140 AdjustAPSInt(ConvVal, UnpromotedWidth, UnpromotedSign);
1141 AdjustAPSInt(ConvVal, Val.getBitWidth(), Val.isSigned());
1146 S.
Diag(Loc, diag::warn_case_value_overflow) <<
toString(Val, 10)
1157 const Expr *CaseExpr,
1158 EnumValsTy::iterator &EI,
1159 EnumValsTy::iterator &EIEnd,
1160 const llvm::APSInt &Val) {
1166 if (
const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
1175 if (ED->
hasAttr<FlagEnumAttr>())
1178 while (EI != EIEnd && EI->first < Val)
1181 if (EI != EIEnd && EI->first == Val)
1194 if (!CondEnumType || !CaseEnumType)
1201 if (!CaseEnumType->getDecl()->getIdentifier() &&
1202 !CaseEnumType->getDecl()->getTypedefNameForAnonDecl())
1208 S.
Diag(Case->
getExprLoc(), diag::warn_comparison_of_mixed_enum_types_switch)
1218 assert(SS ==
getCurFunction()->SwitchStack.back().getPointer() &&
1219 "switch stack missing push/pop!");
1224 SS->
setBody(BodyStmt, SwitchLoc);
1238 const Expr *CondExprBeforePromotion = CondExpr;
1244 bool HasDependentValue
1253 unsigned CondWidthBeforePromotion
1255 bool CondIsSignedBeforePromotion
1262 CaseValsTy CaseVals;
1265 typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy;
1266 CaseRangesTy CaseRanges;
1270 bool CaseListIsErroneous =
false;
1275 if (
DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
1276 if (TheDefaultStmt) {
1277 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
1284 CaseListIsErroneous =
true;
1286 TheDefaultStmt = DS;
1294 HasDependentValue =
true;
1300 const Expr *LoBeforePromotion = Lo;
1307 CondIsSignedBeforePromotion);
1319 HasDependentValue =
true;
1322 CaseRanges.push_back(std::make_pair(LoVal, CS));
1324 CaseVals.push_back(std::make_pair(LoVal, CS));
1328 if (!HasDependentValue) {
1331 llvm::APSInt ConstantCondValue;
1332 bool HasConstantCond =
false;
1333 if (!TheDefaultStmt) {
1338 ConstantCondValue =
Result.Val.getInt();
1339 assert(!HasConstantCond ||
1340 (ConstantCondValue.getBitWidth() == CondWidth &&
1341 ConstantCondValue.isSigned() == CondIsSigned));
1343 bool ShouldCheckConstantCond = HasConstantCond;
1348 if (!CaseVals.empty()) {
1349 for (
unsigned i = 0, e = CaseVals.size(); i != e; ++i) {
1350 if (ShouldCheckConstantCond &&
1351 CaseVals[i].first == ConstantCondValue)
1352 ShouldCheckConstantCond =
false;
1354 if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) {
1357 StringRef PrevString, CurrString;
1360 if (
DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(PrevCase)) {
1361 PrevString = DeclRef->getDecl()->getName();
1363 if (
DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(CurrCase)) {
1364 CurrString = DeclRef->getDecl()->getName();
1367 CaseVals[i-1].first.toString(CaseValStr);
1369 if (PrevString == CurrString)
1370 Diag(CaseVals[i].second->getLHS()->getBeginLoc(),
1371 diag::err_duplicate_case)
1372 << (PrevString.empty() ? CaseValStr.str() : PrevString);
1374 Diag(CaseVals[i].second->getLHS()->getBeginLoc(),
1375 diag::err_duplicate_case_differing_expr)
1376 << (PrevString.empty() ? CaseValStr.str() : PrevString)
1377 << (CurrString.empty() ? CaseValStr.str() : CurrString)
1380 Diag(CaseVals[i - 1].second->getLHS()->getBeginLoc(),
1381 diag::note_duplicate_case_prev);
1384 CaseListIsErroneous =
true;
1391 if (!CaseRanges.empty()) {
1394 llvm::stable_sort(CaseRanges);
1397 std::vector<llvm::APSInt> HiVals;
1398 for (
unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
1399 llvm::APSInt &LoVal = CaseRanges[i].first;
1400 CaseStmt *CR = CaseRanges[i].second;
1403 const Expr *HiBeforePromotion = Hi;
1410 CondWidthBeforePromotion, CondIsSignedBeforePromotion);
1416 if (LoVal > HiVal) {
1419 CaseRanges.erase(CaseRanges.begin()+i);
1425 if (ShouldCheckConstantCond &&
1426 LoVal <= ConstantCondValue &&
1427 ConstantCondValue <= HiVal)
1428 ShouldCheckConstantCond =
false;
1430 HiVals.push_back(HiVal);
1436 for (
unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
1437 llvm::APSInt &CRLo = CaseRanges[i].first;
1438 llvm::APSInt &CRHi = HiVals[i];
1439 CaseStmt *CR = CaseRanges[i].second;
1444 llvm::APSInt OverlapVal(32);
1448 CaseValsTy::iterator I =
1449 llvm::lower_bound(CaseVals, CRLo, CaseCompareFunctor());
1450 if (I != CaseVals.end() && I->first < CRHi) {
1451 OverlapVal = I->first;
1452 OverlapStmt = I->second;
1456 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
1457 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
1458 OverlapVal = (I-1)->first;
1459 OverlapStmt = (I-1)->second;
1464 if (i && CRLo <= HiVals[i-1]) {
1465 OverlapVal = HiVals[i-1];
1466 OverlapStmt = CaseRanges[i-1].second;
1474 diag::note_duplicate_case_prev);
1477 CaseListIsErroneous =
true;
1483 if (!CaseListIsErroneous && !CaseListIsIncomplete &&
1484 ShouldCheckConstantCond) {
1487 Diag(CondExpr->
getExprLoc(), diag::warn_missing_case_for_condition)
1500 if (!CaseListIsErroneous && !CaseListIsIncomplete && !HasConstantCond &&
1509 llvm::APSInt Val = EDI->getInitVal();
1511 EnumVals.push_back(std::make_pair(Val, EDI));
1514 auto EI = EnumVals.begin(), EIEnd =
1515 std::unique(EnumVals.begin(), EnumVals.end(),
EqEnumVals);
1518 for (CaseValsTy::const_iterator CI = CaseVals.begin();
1519 CI != CaseVals.end(); CI++) {
1520 Expr *CaseExpr = CI->second->getLHS();
1524 << CondTypeBeforePromotion;
1528 EI = EnumVals.begin();
1529 for (CaseRangesTy::const_iterator RI = CaseRanges.begin();
1530 RI != CaseRanges.end(); RI++) {
1531 Expr *CaseExpr = RI->second->getLHS();
1535 << CondTypeBeforePromotion;
1538 RI->second->getRHS()->EvaluateKnownConstInt(
Context);
1541 CaseExpr = RI->second->getRHS();
1545 << CondTypeBeforePromotion;
1549 auto CI = CaseVals.begin();
1550 auto RI = CaseRanges.begin();
1551 bool hasCasesNotInSwitch =
false;
1555 for (EI = EnumVals.begin(); EI != EIEnd; EI++) {
1557 switch (EI->second->getAvailability()) {
1570 if (EI->second->hasAttr<UnusedAttr>())
1574 while (CI != CaseVals.end() && CI->first < EI->first)
1577 if (CI != CaseVals.end() && CI->first == EI->first)
1581 for (; RI != CaseRanges.end(); RI++) {
1583 RI->second->getRHS()->EvaluateKnownConstInt(
Context);
1585 if (EI->first <= Hi)
1589 if (RI == CaseRanges.end() || EI->first < RI->first) {
1590 hasCasesNotInSwitch =
true;
1591 UnhandledNames.push_back(EI->second->getDeclName());
1595 if (TheDefaultStmt && UnhandledNames.empty() && ED->
isClosedNonFlag())
1599 if (!UnhandledNames.empty()) {
1601 ? diag::warn_def_missing_case
1602 : diag::warn_missing_case)
1605 for (
size_t I = 0, E = std::min(UnhandledNames.size(), (
size_t)3);
1607 DB << UnhandledNames[I];
1610 if (!hasCasesNotInSwitch)
1617 diag::warn_empty_switch_body);
1621 if (CaseListIsErroneous)
1644 const EnumDecl *ED = ET->getDecl();
1649 if (ED->
hasAttr<FlagEnumAttr>()) {
1661 llvm::APSInt Val = EDI->getInitVal();
1663 EnumVals.push_back(std::make_pair(Val, EDI));
1665 if (EnumVals.empty())
1668 EnumValsTy::iterator EIend =
1669 std::unique(EnumVals.begin(), EnumVals.end(),
EqEnumVals);
1672 EnumValsTy::const_iterator EI = EnumVals.begin();
1673 while (EI != EIend && EI->first < RhsVal)
1675 if (EI == EIend || EI->first != RhsVal) {
1690 auto CondVal = Cond.
get();
1691 CheckBreakContinueBinding(CondVal.second);
1693 if (CondVal.second &&
1694 !
Diags.
isIgnored(diag::warn_comma_operator, CondVal.second->getExprLoc()))
1695 CommaVisitor(*this).Visit(CondVal.second);
1697 if (isa<NullStmt>(Body))
1701 WhileLoc, LParenLoc, RParenLoc);
1708 assert(Cond &&
"ActOnDoStmt(): missing expression");
1710 CheckBreakContinueBinding(Cond);
1714 Cond = CondResult.
get();
1719 Cond = CondResult.
get();
1724 CommaVisitor(*this).Visit(Cond);
1726 return new (
Context)
DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen);
1731 using DeclSetVector =
1732 llvm::SetVector<VarDecl *, llvm::SmallVector<VarDecl *, 8>,
1739 DeclSetVector &Decls;
1745 DeclExtractor(
Sema &S, DeclSetVector &Decls,
1747 Inherited(S.Context),
1752 bool isSimple() {
return Simple; }
1820 DeclSetVector &Decls;
1826 DeclMatcher(
Sema &S, DeclSetVector &Decls,
Stmt *Statement) :
1827 Inherited(S.Context), Decls(Decls), FoundDecl(
false) {
1828 if (!Statement)
return;
1852 void CheckLValueToRValueCast(
Expr *E) {
1855 if (isa<DeclRefExpr>(E)) {
1860 Visit(CO->getCond());
1861 CheckLValueToRValueCast(CO->getTrueExpr());
1862 CheckLValueToRValueCast(CO->getFalseExpr());
1867 dyn_cast<BinaryConditionalOperator>(E)) {
1868 CheckLValueToRValueCast(BCO->getOpaqueValue()->getSourceExpr());
1869 CheckLValueToRValueCast(BCO->getFalseExpr());
1878 if (Decls.count(VD))
1886 if (
auto *OVE = dyn_cast<OpaqueValueExpr>(S))
1888 Visit(OVE->getSourceExpr());
1894 bool FoundDeclInUse() {
return FoundDecl; }
1898 void CheckForLoopConditionalStatement(
Sema &S,
Expr *Second,
1901 if (!Second)
return;
1908 DeclSetVector Decls;
1910 DeclExtractor DE(S, Decls, Ranges);
1914 if (!DE.isSimple())
return;
1917 if (Decls.size() == 0)
return;
1920 for (
auto *VD : Decls)
1924 if (DeclMatcher(S, Decls, Second).FoundDeclInUse() ||
1925 DeclMatcher(S, Decls, Third).FoundDeclInUse() ||
1926 DeclMatcher(S, Decls, Body).FoundDeclInUse())
1930 if (Decls.size() > 4) {
1934 for (
auto *VD : Decls)
1938 for (
auto Range : Ranges)
1941 S.
Diag(Ranges.begin()->getBegin(), PDiag);
1946 bool ProcessIterationStmt(
Sema &S,
Stmt* Statement,
bool &Increment,
1948 if (
auto Cleanups = dyn_cast<ExprWithCleanups>(Statement))
1949 if (!Cleanups->cleanupsHaveSideEffects())
1952 if (
UnaryOperator *UO = dyn_cast<UnaryOperator>(Statement)) {
1953 switch (UO->getOpcode()) {
1954 default:
return false;
1964 DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr());
1972 default:
return false;
1980 DRE = dyn_cast<DeclRefExpr>(
Call->getArg(0));
1992 bool InSwitch =
false;
1995 BreakContinueFinder(
Sema &S,
const Stmt* Body) :
1996 Inherited(S.Context) {
2006 void VisitBreakStmt(
const BreakStmt* E) {
2012 if (
const Stmt *Init = S->getInit())
2014 if (
const Stmt *CondVar = S->getConditionVariableDeclStmt())
2016 if (
const Stmt *Cond = S->getCond())
2021 if (
const Stmt *Body = S->getBody())
2026 void VisitForStmt(
const ForStmt *S) {
2029 if (
const Stmt *Init = S->getInit())
2033 void VisitWhileStmt(
const WhileStmt *) {
2038 void VisitDoStmt(
const DoStmt *) {
2046 if (
const Stmt *Init = S->getInit())
2048 if (
const Stmt *Range = S->getRangeStmt())
2050 if (
const Stmt *
Begin = S->getBeginStmt())
2052 if (
const Stmt *End = S->getEndStmt())
2059 if (
const Stmt *Element = S->getElement())
2061 if (
const Stmt *Collection = S->getCollection())
2065 bool ContinueFound() {
return ContinueLoc.
isValid(); }
2066 bool BreakFound() {
return BreakLoc.
isValid(); }
2077 void CheckForRedundantIteration(
Sema &S,
Expr *Third,
Stmt *Body) {
2079 if (!Body || !Third)
return;
2089 if (!LastStmt)
return;
2091 bool LoopIncrement, LastIncrement;
2094 if (!ProcessIterationStmt(S, Third, LoopIncrement, LoopDRE))
return;
2095 if (!ProcessIterationStmt(S, LastStmt, LastIncrement, LastDRE))
return;
2099 if (LoopIncrement != LastIncrement ||
2102 if (BreakContinueFinder(S, Body).ContinueFound())
return;
2105 << LastDRE->
getDecl() << LastIncrement;
2113void Sema::CheckBreakContinueBinding(
Expr *E) {
2116 BreakContinueFinder BCFinder(*
this, E);
2118 if (BCFinder.BreakFound() && BreakParent) {
2120 Diag(BCFinder.GetBreakLoc(), diag::warn_break_binds_to_switch);
2122 Diag(BCFinder.GetBreakLoc(), diag::warn_loop_ctrl_binds_to_inner)
2126 Diag(BCFinder.GetContinueLoc(), diag::warn_loop_ctrl_binds_to_inner)
2143 const Decl *NonVarSeen =
nullptr;
2144 bool VarDeclSeen =
false;
2145 for (
auto *DI : DS->decls()) {
2146 if (
VarDecl *VD = dyn_cast<VarDecl>(DI)) {
2149 Diag(DI->getLocation(), diag::err_non_local_variable_decl_in_for);
2150 DI->setInvalidDecl();
2152 }
else if (!NonVarSeen) {
2162 if (NonVarSeen && !VarDeclSeen)
2167 CheckBreakContinueBinding(Second.
get().second);
2168 CheckBreakContinueBinding(third.
get());
2170 if (!Second.
get().first)
2171 CheckForLoopConditionalStatement(*
this, Second.
get().second, third.
get(),
2173 CheckForRedundantIteration(*
this, third.
get(), Body);
2175 if (Second.
get().second &&
2177 Second.
get().second->getExprLoc()))
2178 CommaVisitor(*this).Visit(Second.
get().second);
2181 if (isa<NullStmt>(Body))
2186 Body, ForLoc, LParenLoc, RParenLoc);
2214 collection = result.
get();
2223 collection = result.
get();
2230 return Diag(forLoc, diag::err_collection_expr_type)
2243 diag::err_arc_collection_forward, collection)
2247 }
else if (iface || !objectType->
qual_empty()) {
2270 Diag(forLoc, diag::warn_collection_expr_type)
2293 if (!DS->isSingleDecl())
2295 diag::err_toomany_element_decls));
2297 VarDecl *D = dyn_cast<VarDecl>(DS->getSingleDecl());
2307 diag::err_non_local_variable_decl_in_for));
2313 Expr *DeducedInit = &OpaqueId;
2320 if (FirstType.
isNull()) {
2330 Diag(Loc, diag::warn_auto_var_is_id)
2339 Diag(
First->getBeginLoc(), diag::err_selector_element_not_lvalue)
2340 <<
First->getSourceRange());
2342 FirstType =
static_cast<Expr*
>(
First)->getType();
2344 Diag(ForLoc, diag::err_selector_element_const_type)
2345 << FirstType <<
First->getSourceRange();
2350 return StmtError(
Diag(ForLoc, diag::err_selector_element_type)
2351 << FirstType <<
First->getSourceRange());
2357 CollectionExprResult =
2363 nullptr, ForLoc, RParenLoc);
2370 if (
Decl->getType()->isUndeducedType()) {
2382 if (!isa<InitListExpr>(Init) && Init->getType()->isVoidType()) {
2383 SemaRef.
Diag(Loc, DiagID) << Init->getType();
2387 Decl->getTypeSourceInfo()->getTypeLoc(), Init, InitType, Info);
2389 SemaRef.
Diag(Loc, DiagID) << Init->getType();
2396 Decl->setType(InitType);
2414enum BeginEndFunction {
2423void NoteForRangeBeginEndFunction(
Sema &SemaRef,
Expr *E,
2424 BeginEndFunction BEF) {
2425 CallExpr *CE = dyn_cast<CallExpr>(E);
2433 std::string Description;
2434 bool IsTemplate =
false;
2441 SemaRef.
Diag(Loc, diag::note_for_range_begin_end)
2442 << BEF << IsTemplate << Description << E->
getType();
2494 return Diag(InitStmt->
getBeginLoc(), diag::err_objc_for_range_init_stmt)
2500 assert(DS &&
"first part of for range not a decl stmt");
2527 const auto DepthStr = std::to_string(S->getDepth() / 2);
2529 VarDecl *RangeVar = BuildForRangeVarDecl(*
this, RangeLoc,
2531 std::string(
"__range") + DepthStr);
2533 diag::err_for_range_deduction_failure)) {
2548 ForLoc, CoawaitLoc, InitStmt, ColonLoc, RangeDecl.
get(),
2550 nullptr,
nullptr, DS, RParenLoc, Kind);
2573 ExprResult *EndExpr, BeginEndFunction *BEF) {
2583 auto BuildBegin = [&] {
2587 BeginMemberLookup, CandidateSet,
2588 BeginRange, BeginExpr);
2593 << ColonLoc << BEF_begin << BeginRange->
getType();
2606 diag::err_for_range_iter_deduction_failure)) {
2607 NoteForRangeBeginEndFunction(SemaRef, BeginExpr->
get(), *BEF);
2613 auto BuildEnd = [&] {
2617 EndMemberLookup, CandidateSet,
2622 << ColonLoc << BEF_end << EndRange->
getType();
2626 diag::err_for_range_iter_deduction_failure)) {
2627 NoteForRangeBeginEndFunction(SemaRef, EndExpr->
get(), *BEF);
2647 if (BeginMemberLookup.
empty() != EndMemberLookup.
empty()) {
2652 auto BuildNonmember = [&](
2662 switch (BuildFound()) {
2669 SemaRef.
PDiag(diag::err_for_range_invalid)
2670 << BeginRange->
getType() << BEFFound),
2677 diag::note_for_range_member_begin_end_ignored)
2678 << BeginRange->
getType() << BEFFound;
2682 llvm_unreachable(
"unexpected ForRangeStatus");
2684 if (BeginMemberLookup.
empty())
2685 return BuildNonmember(BEF_end, EndMemberLookup, BuildEnd, BuildBegin);
2686 return BuildNonmember(BEF_begin, BeginMemberLookup, BuildBegin, BuildEnd);
2718 AdjustedRange = SemaRef.
BuildUnaryOp(S, RangeLoc, UO_Deref, Range);
2723 S, ForLoc, CoawaitLoc, InitStmt, LoopVarDecl, ColonLoc,
2732 SemaRef.
Diag(RangeLoc, diag::err_for_range_dereference)
2735 S, ForLoc, CoawaitLoc, InitStmt, LoopVarDecl, ColonLoc,
2758 DeclStmt *RangeDS = cast<DeclStmt>(RangeDecl);
2762 DeclStmt *LoopVarDS = cast<DeclStmt>(LoopVarDecl);
2776 if (
auto *DD = dyn_cast<DecompositionDecl>(LoopVar))
2777 for (
auto *Binding : DD->bindings())
2781 }
else if (!BeginDeclStmt.
get()) {
2800 QualType RangeType = Range->getType();
2803 diag::err_for_range_incomplete_type))
2808 const auto DepthStr = std::to_string(S->getDepth() / 2);
2810 std::string(
"__begin") + DepthStr);
2812 std::string(
"__end") + DepthStr);
2823 BeginExpr = BeginRangeRef;
2830 diag::err_for_range_iter_deduction_failure)) {
2831 NoteForRangeBeginEndFunction(*
this, BeginExpr.
get(), BEF_begin);
2841 dyn_cast<VariableArrayType>(UnqAT)) {
2868 VAT->desugar(), RangeLoc))
2879 VAT->getElementType(), RangeLoc))
2887 SizeOfVLAExprR.
get(), SizeOfEachElementExprR.
get());
2894 llvm_unreachable(
"Unexpected array type in for-range");
2898 EndExpr =
ActOnBinOp(S, ColonLoc, tok::plus, EndRangeRef.
get(),
2903 diag::err_for_range_iter_deduction_failure)) {
2904 NoteForRangeBeginEndFunction(*
this, EndExpr.
get(), BEF_end);
2910 BeginEndFunction BEFFailure;
2912 *
this, BeginRangeRef.
get(), EndRangeRef.
get(), RangeType, BeginVar,
2913 EndVar, ColonLoc, CoawaitLoc, &CandidateSet, &BeginExpr, &EndExpr,
2917 BEFFailure == BEF_begin) {
2920 if (
DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Range)) {
2922 QualType ArrayTy = PVD->getOriginalType();
2923 QualType PointerTy = PVD->getType();
2925 Diag(Range->getBeginLoc(), diag::err_range_on_array_parameter)
2926 << RangeLoc << PVD << ArrayTy << PointerTy;
2927 Diag(PVD->getLocation(), diag::note_declared_at);
2936 CoawaitLoc, InitStmt,
2937 LoopVarDecl, ColonLoc,
2946 Expr *Range = BEFFailure ? EndRangeRef.
get() : BeginRangeRef.
get();
2949 PDiag(diag::err_for_range_invalid)
2950 << RangeLoc << Range->getType()
2960 "invalid range expression in for loop");
2967 ? diag::warn_for_range_begin_end_types_differ
2968 : diag::ext_for_range_begin_end_types_differ)
2969 << BeginType << EndType;
2970 NoteForRangeBeginEndFunction(*
this, BeginExpr.
get(), BEF_begin);
2971 NoteForRangeBeginEndFunction(*
this, EndExpr.
get(), BEF_end);
2991 NotEqExpr =
ActOnBinOp(S, ColonLoc, tok::exclaimequal,
2992 BeginRef.
get(), EndRef.
get());
2999 Diag(RangeLoc, diag::note_for_range_invalid_iterator)
3000 << RangeLoc << 0 << BeginRangeRef.
get()->
getType();
3001 NoteForRangeBeginEndFunction(*
this, BeginExpr.
get(), BEF_begin);
3003 NoteForRangeBeginEndFunction(*
this, EndExpr.
get(), BEF_end);
3014 if (!IncrExpr.isInvalid() && CoawaitLoc.
isValid())
3019 if (!IncrExpr.isInvalid())
3021 if (IncrExpr.isInvalid()) {
3022 Diag(RangeLoc, diag::note_for_range_invalid_iterator)
3023 << RangeLoc << 2 << BeginRangeRef.
get()->
getType() ;
3024 NoteForRangeBeginEndFunction(*
this, BeginExpr.
get(), BEF_begin);
3036 Diag(RangeLoc, diag::note_for_range_invalid_iterator)
3037 << RangeLoc << 1 << BeginRangeRef.
get()->
getType();
3038 NoteForRangeBeginEndFunction(*
this, BeginExpr.
get(), BEF_begin);
3048 NoteForRangeBeginEndFunction(*
this, BeginExpr.
get(), BEF_begin);
3063 InitStmt, RangeDS, cast_or_null<DeclStmt>(BeginDeclStmt.
get()),
3064 cast_or_null<DeclStmt>(EndDeclStmt.
get()), NotEqExpr.
get(),
3065 IncrExpr.get(), LoopVarDS,
nullptr, ForLoc, CoawaitLoc,
3066 ColonLoc, RParenLoc);
3096 if (
auto Cleanups = dyn_cast<ExprWithCleanups>(InitExpr))
3097 if (!Cleanups->cleanupsHaveSideEffects())
3098 InitExpr = Cleanups->getSubExpr();
3101 dyn_cast<MaterializeTemporaryExpr>(InitExpr);
3111 while (!isa<CXXOperatorCallExpr>(E) && !isa<UnaryOperator>(E)) {
3115 const MemberExpr *ME = cast<MemberExpr>(Call->getCallee());
3125 if (isa<UnaryOperator>(E)) {
3132 ReferenceReturnType = ReturnType;
3135 if (!ReferenceReturnType.
isNull()) {
3140 diag::warn_for_range_const_ref_binds_temp_built_from_ref)
3141 << VD << VariableType << ReferenceReturnType;
3154 SemaRef.
Diag(VD->
getLocation(), diag::warn_for_range_ref_binds_ret_temp)
3155 << VD << RangeInitType;
3168 return RD->hasAttr<TrivialABIAttr>();
3185 if (!CE->getConstructor()->isCopyConstructor())
3187 }
else if (
const CastExpr *CE = dyn_cast<CastExpr>(InitExpr)) {
3188 if (CE->getCastKind() != CK_LValueToRValue)
3206 << VD << VariableType;
3227 diag::warn_for_range_const_ref_binds_temp_built_from_ref,
3254 ForStmt->getRangeInit()->getType());
3268 if (isa<ObjCForCollectionStmt>(S))
3275 diag::warn_empty_range_based_for_body);
3318 const Scope &DestScope) {
3321 S.
Diag(Loc, diag::warn_jump_out_of_seh_finally);
3330 return StmtError(
Diag(ContinueLoc, diag::err_continue_not_in_loop));
3332 if (S->isConditionVarScope()) {
3336 return StmtError(
Diag(ContinueLoc, diag::err_continue_from_cond_var_init));
3348 return StmtError(
Diag(BreakLoc, diag::err_break_not_in_loop_or_switch));
3350 if (S->isOpenMPLoopScope())
3351 return StmtError(
Diag(BreakLoc, diag::err_omp_loop_cannot_use_stmt)
3378 const auto *DR = dyn_cast<DeclRefExpr>(E->
IgnoreParens());
3379 if (!DR || DR->refersToEnclosingVariableOrCapture())
3381 const auto *VD = dyn_cast<VarDecl>(DR->getDecl());
3410 if (VD->
getKind() == Decl::ParmVar)
3412 else if (VD->
getKind() != Decl::Var)
3425 if (VD->
hasAttr<BlocksAttr>())
3470 auto invalidNRVO = [&] {
3479 if ((ReturnType->
getTypeClass() == Type::TypeClass::Auto &&
3482 return invalidNRVO();
3488 return invalidNRVO();
3508 const auto *Step = llvm::find_if(Seq.
steps(), [](
const auto &Step) {
3509 return Step.Kind == InitializationSequence::SK_ConstructorInitialization ||
3510 Step.Kind == InitializationSequence::SK_UserConversion;
3513 const auto *FD = Step->Function.Function;
3514 if (isa<CXXConstructorDecl>(FD)
3516 : cast<CXXMethodDecl>(FD)->getRefQualifier() ==
RQ_None)
3530 bool SupressSimplerImplicitMoves) {
3536 Expr *InitExpr = &AsRvalue;
3538 Value->getBeginLoc());
3574 bool SupressSimplerImplicitMoves) {
3580 bool HasDeducedReturnType =
3590 RetValExp = ER.
get();
3596 if (HasDeducedReturnType) {
3609 assert(AT &&
"lost auto type from lambda return type");
3621 if (RetValExp && !isa<InitListExpr>(RetValExp)) {
3625 RetValExp =
Result.get();
3640 Diag(ReturnLoc, diag::err_lambda_return_init_list)
3654 if (
auto *CurBlock = dyn_cast<BlockScopeInfo>(CurCap)) {
3656 Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr);
3659 }
else if (
auto *CurRegion = dyn_cast<CapturedRegionScopeInfo>(CurCap)) {
3660 Diag(ReturnLoc, diag::err_return_in_captured_stmt) << CurRegion->getRegionName();
3663 assert(CurLambda &&
"unknown kind of captured scope");
3667 Diag(ReturnLoc, diag::err_noreturn_lambda_has_return_expr);
3679 if (RetValExp && !isa<InitListExpr>(RetValExp) &&
3685 Diag(ReturnLoc, diag::ext_return_has_void_expr) <<
"literal" << 2;
3687 Diag(ReturnLoc, diag::err_return_block_has_expr);
3688 RetValExp =
nullptr;
3691 }
else if (!RetValExp) {
3692 return StmtError(
Diag(ReturnLoc, diag::err_block_return_missing_expr));
3705 Entity, NRInfo, RetValExp, SupressSimplerImplicitMoves);
3710 RetValExp = Res.
get();
3711 CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc);
3719 RetValExp = ER.
get();
3749class LocalTypedefNameReferencer
3752 LocalTypedefNameReferencer(
Sema &S) : S(S) {}
3757bool LocalTypedefNameReferencer::VisitRecordType(
const RecordType *RT) {
3758 auto *R = dyn_cast<CXXRecordDecl>(RT->
getDecl());
3759 if (!R || !R->isLocalClass() || !R->isLocalClass()->isExternallyVisible() ||
3760 R->isDependentType())
3762 for (
auto *TmpD : R->decls())
3763 if (
auto *T = dyn_cast<TypedefNameDecl>(TmpD))
3764 if (T->getAccess() !=
AS_private || R->hasFriends())
3788 if (RetExpr && isa<InitListExpr>(RetExpr)) {
3793 : diag::err_auto_fn_return_init_list)
3803 assert(AT->
isDeduced() &&
"should have deduced to dependent type");
3817 Diag(ReturnLoc, diag::err_auto_fn_return_void_but_not_auto)
3845 Diag(ReturnLoc, diag::err_typecheck_missing_return_type_incompatible)
3848 Diag(ReturnLoc, diag::err_auto_fn_different_deductions)
3862 LocalTypedefNameReferencer(*this).TraverseType(RetExpr->
getType());
3885 RetValExp,
nullptr,
true);
3894 const_cast<VarDecl *
>(cast<ReturnStmt>(R.
get())->getNRVOCandidate());
3918 bool AllowRecovery) {
3926 bool SupressSimplerImplicitMoves =
3934 SupressSimplerImplicitMoves);
3938 const AttrVec *Attrs =
nullptr;
3939 bool isObjCMethod =
false;
3946 Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) << FD;
3947 if (FD->
isMain() && RetValExp)
3948 if (isa<CXXBoolLiteralExpr>(RetValExp))
3949 Diag(ReturnLoc, diag::warn_main_returns_bool_literal)
3951 if (FD->
hasAttr<CmseNSEntryAttr>() && RetValExp) {
3958 FnRetType = MD->getReturnType();
3959 isObjCMethod =
true;
3961 Attrs = &MD->getAttrs();
3962 if (MD->hasRelatedResultType() && MD->getClassInterface()) {
3981 RetValExp = ER.
get();
4007 AT->isDeduced() ? FnRetType :
QualType());
4008 if (Recovery.isInvalid())
4010 RetValExp = Recovery.get();
4026 if (
auto *ILE = dyn_cast<InitListExpr>(RetValExp)) {
4031 int FunctionKind = 0;
4032 if (isa<ObjCMethodDecl>(CurDecl))
4034 else if (isa<CXXConstructorDecl>(CurDecl))
4036 else if (isa<CXXDestructorDecl>(CurDecl))
4039 Diag(ReturnLoc, diag::err_return_init_list)
4043 RetValExp = AllowRecovery
4045 ILE->getRBraceLoc(), ILE->inits())
4050 unsigned D = diag::ext_return_has_expr;
4053 if (isa<CXXConstructorDecl>(CurDecl) ||
4054 isa<CXXDestructorDecl>(CurDecl))
4055 D = diag::err_ctor_dtor_returns_void;
4057 D = diag::ext_return_has_void_expr;
4064 RetValExp =
Result.get();
4069 if (D == diag::err_ctor_dtor_returns_void) {
4071 Diag(ReturnLoc, D) << CurDecl << isa<CXXDestructorDecl>(CurDecl)
4075 else if (D != diag::ext_return_has_void_expr ||
4079 int FunctionKind = 0;
4080 if (isa<ObjCMethodDecl>(CurDecl))
4082 else if (isa<CXXConstructorDecl>(CurDecl))
4084 else if (isa<CXXDestructorDecl>(CurDecl))
4097 RetValExp = ER.
get();
4103 }
else if (!RetValExp && !HasDependentReturnType) {
4110 Diag(ReturnLoc, diag::err_constexpr_return_missing_expr)
4116 unsigned DiagID =
getLangOpts().C99 ? diag::ext_return_missing_expr
4117 : diag::warn_return_missing_expr;
4121 "Not in a FunctionDecl or ObjCMethodDecl?");
4122 bool IsMethod = FD ==
nullptr;
4125 Diag(ReturnLoc, DiagID) << ND << IsMethod;
4131 assert(RetValExp || HasDependentReturnType);
4132 QualType RetType = RelatedRetType.
isNull() ? FnRetType : RelatedRetType;
4145 Entity, NRInfo, RetValExp, SupressSimplerImplicitMoves);
4148 RetValExp->
getEndLoc(), RetValExp, RetType);
4159 if (!RelatedRetType.
isNull()) {
4170 CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc, isObjCMethod, Attrs,
4179 RetValExp = ER.
get();
4186 if (
Result->getNRVOCandidate())
4199 VarDecl *Var = cast_or_null<VarDecl>(Parm);
4215 Diag(AtLoc, diag::err_objc_exceptions_disabled) <<
"@try";
4220 Diag(AtLoc, diag::err_mixing_cxx_try_seh_try) << 1;
4225 unsigned NumCatchStmts = CatchStmts.size();
4227 NumCatchStmts, Finally);
4247 return StmtError(
Diag(AtLoc, diag::err_objc_throw_expects_object)
4259 Diag(AtLoc, diag::err_objc_exceptions_disabled) <<
"@throw";
4264 Scope *AtCatchParent = CurScope;
4266 AtCatchParent = AtCatchParent->
getParent();
4268 return StmtError(
Diag(AtLoc, diag::err_rethrow_used_outside_catch));
4278 operand = result.
get();
4282 if (!
type->isDependentType() &&
4283 !
type->isObjCObjectPointerType()) {
4288 diag::err_incomplete_receiver_type))
4289 return Diag(atLoc, diag::err_objc_synchronized_expects_object)
4296 return Diag(atLoc, diag::err_objc_synchronized_expects_object)
4299 operand = result.
get();
4301 return Diag(atLoc, diag::err_objc_synchronized_expects_object)
4323 Stmt *HandlerBlock) {
4326 CXXCatchStmt(CatchLoc, cast_or_null<VarDecl>(ExDecl), HandlerBlock);
4336class CatchHandlerType {
4338 unsigned IsPointer : 1;
4342 friend struct llvm::DenseMapInfo<CatchHandlerType>;
4343 enum Unique { ForDenseMap };
4344 CatchHandlerType(
QualType QT, Unique) : QT(QT), IsPointer(
false) {}
4362 CatchHandlerType(
QualType QT,
bool IsPointer)
4363 : QT(QT), IsPointer(IsPointer) {}
4365 QualType underlying()
const {
return QT; }
4366 bool isPointer()
const {
return IsPointer; }
4368 friend bool operator==(
const CatchHandlerType &LHS,
4369 const CatchHandlerType &RHS) {
4371 if (LHS.IsPointer != RHS.IsPointer)
4374 return LHS.QT == RHS.QT;
4380template <>
struct DenseMapInfo<CatchHandlerType> {
4382 return CatchHandlerType(DenseMapInfo<QualType>::getEmptyKey(),
4383 CatchHandlerType::ForDenseMap);
4387 return CatchHandlerType(DenseMapInfo<QualType>::getTombstoneKey(),
4388 CatchHandlerType::ForDenseMap);
4392 return DenseMapInfo<QualType>::getHashValue(
Base.underlying());
4396 const CatchHandlerType &RHS) {
4403class CatchTypePublicBases {
4404 const llvm::DenseMap<QualType, CXXCatchStmt *> &TypesToCheck;
4411 CatchTypePublicBases(
const llvm::DenseMap<QualType, CXXCatchStmt *> &T,
4413 : TypesToCheck(T), FoundHandler(nullptr), TestAgainstType(QT) {}
4415 CXXCatchStmt *getFoundHandler()
const {
return FoundHandler; }
4416 QualType getFoundHandlerType()
const {
return FoundHandlerType; }
4419 if (S->getAccessSpecifier() == AccessSpecifier::AS_public) {
4420 QualType Check = S->getType().getCanonicalType();
4421 const auto &M = TypesToCheck;
4422 auto I = M.find(Check);
4434 if (I->second->getCaughtType()->isPointerType() ==
4436 FoundHandler = I->second;
4437 FoundHandlerType = Check;
4455 targetDiag(TryLoc, diag::err_exceptions_disabled) <<
"try";
4464 Diag(TryLoc, diag::err_omp_simd_region_cannot_use_stmt) <<
"try";
4470 Diag(TryLoc, diag::err_mixing_cxx_try_seh_try) << 0;
4474 const unsigned NumHandlers = Handlers.size();
4475 assert(!Handlers.empty() &&
4476 "The parser shouldn't call this if there are no handlers.");
4478 llvm::DenseMap<QualType, CXXCatchStmt *> HandledBaseTypes;
4479 llvm::DenseMap<CatchHandlerType, CXXCatchStmt *> HandledTypes;
4480 for (
unsigned i = 0; i < NumHandlers; ++i) {
4487 if (i < NumHandlers - 1)
4501 QualType Underlying = HandlerCHT.underlying();
4503 if (!RD->hasDefinition())
4511 Paths.setOrigin(RD);
4512 CatchTypePublicBases CTPB(HandledBaseTypes,
4514 if (RD->lookupInBases(CTPB, Paths)) {
4516 if (!Paths.isAmbiguous(
4519 diag::warn_exception_caught_by_earlier_handler)
4522 diag::note_previous_exception_handler)
4534 auto R = HandledTypes.insert(
4539 diag::warn_exception_caught_by_earlier_handler)
4542 diag::note_previous_exception_handler)
4554 assert(TryBlock && Handler);
4581 Diag(TryLoc, diag::err_seh_try_outside_functions);
4585 Diag(TryLoc, diag::err_seh_try_unsupported);
4592 assert(FilterExpr &&
Block);
4596 Diag(FilterExpr->
getExprLoc(), diag::err_filter_expression_integral)
4618 Scope *SEHTryParent = CurScope;
4620 SEHTryParent = SEHTryParent->
getParent();
4622 return StmtError(
Diag(Loc, diag::err_ms___leave_not_in___try));
4635 QualifierLoc, NameInfo,
4636 cast<CompoundStmt>(Nested));
4653 unsigned NumParams) {
4670 assert(NumParams > 0 &&
"CapturedStmt requires context parameter");
4713 CaptureInits.push_back(Init.get());
4720 unsigned NumParams) {
4750 unsigned OpenMPCaptureLevel) {
4756 bool ContextIsFound =
false;
4757 unsigned ParamNum = 0;
4760 I != E; ++I, ++ParamNum) {
4761 if (I->second.isNull()) {
4762 assert(!ContextIsFound &&
4763 "null type has been found already for '__context' parameter");
4773 ContextIsFound =
true;
4783 assert(ContextIsFound &&
"no null type for '__context' parameter");
4784 if (!ContextIsFound) {
4814 Record->setInvalidDecl();
4817 ActOnFields(
nullptr, Record->getLocation(), Record, Fields,
4840 Captures, CaptureInits, CD, RD);
Defines the clang::ASTContext interface.
This file provides some common utility functions for processing Lambda related AST Constructs.
Defines the clang::Expr interface and subclasses for C++ expressions.
Defines the clang::Preprocessor interface.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
static bool CmpEnumVals(const std::pair< llvm::APSInt, EnumConstantDecl * > &lhs, const std::pair< llvm::APSInt, EnumConstantDecl * > &rhs)
CmpEnumVals - Comparison predicate for sorting enumeration values.
static bool FinishForRangeVarDecl(Sema &SemaRef, VarDecl *Decl, Expr *Init, SourceLocation Loc, int DiagID)
Finish building a variable declaration for a for-range statement.
static bool CmpCaseVals(const std::pair< llvm::APSInt, CaseStmt * > &lhs, const std::pair< llvm::APSInt, CaseStmt * > &rhs)
CmpCaseVals - Comparison predicate for sorting case values.
SmallVector< std::pair< llvm::APSInt, EnumConstantDecl * >, 64 > EnumValsTy
static bool ShouldDiagnoseSwitchCaseNotInEnum(const Sema &S, const EnumDecl *ED, const Expr *CaseExpr, EnumValsTy::iterator &EI, EnumValsTy::iterator &EIEnd, const llvm::APSInt &Val)
Returns true if we should emit a diagnostic about this case expression not being a part of the enum u...
static bool DiagnoseUnusedComparison(Sema &S, const Expr *E)
Diagnose unused comparisons, both builtin and overloaded operators.
static bool EqEnumVals(const std::pair< llvm::APSInt, EnumConstantDecl * > &lhs, const std::pair< llvm::APSInt, EnumConstantDecl * > &rhs)
EqEnumVals - Comparison preficate for uniqing enumeration values.
static bool hasDeducedReturnType(FunctionDecl *FD)
Determine whether the declared return type of the specified function contains 'auto'.
static bool ObjCEnumerationCollection(Expr *Collection)
static void DiagnoseForRangeConstVariableCopies(Sema &SemaRef, const VarDecl *VD)
static StmtResult RebuildForRangeWithDereference(Sema &SemaRef, Scope *S, SourceLocation ForLoc, SourceLocation CoawaitLoc, Stmt *InitStmt, Stmt *LoopVarDecl, SourceLocation ColonLoc, Expr *Range, SourceLocation RangeLoc, SourceLocation RParenLoc)
Speculatively attempt to dereference an invalid range expression.
static void checkEnumTypesInSwitchStmt(Sema &S, const Expr *Cond, const Expr *Case)
static void DiagnoseForRangeReferenceVariableCopies(Sema &SemaRef, const VarDecl *VD, QualType RangeInitType)
static void DiagnoseForRangeVariableCopies(Sema &SemaRef, const CXXForRangeStmt *ForStmt)
DiagnoseForRangeVariableCopies - Diagnose three cases and fixes for them.
static bool CheckSimplerImplicitMovesMSVCWorkaround(const Sema &S, const Expr *E)
static bool VerifyInitializationSequenceCXX98(const Sema &S, const InitializationSequence &Seq)
Verify that the initialization sequence that was picked for the first overload resolution is permissi...
static QualType GetTypeBeforeIntegralPromotion(const Expr *&E)
GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of potentially integral-promoted expr...
static Sema::ForRangeStatus BuildNonArrayForRange(Sema &SemaRef, Expr *BeginRange, Expr *EndRange, QualType RangeType, VarDecl *BeginVar, VarDecl *EndVar, SourceLocation ColonLoc, SourceLocation CoawaitLoc, OverloadCandidateSet *CandidateSet, ExprResult *BeginExpr, ExprResult *EndExpr, BeginEndFunction *BEF)
Create the initialization, compare, and increment steps for the range-based for loop expression.
static bool DiagnoseNoDiscard(Sema &S, const WarnUnusedResultAttr *A, SourceLocation Loc, SourceRange R1, SourceRange R2, bool IsCtor)
static bool hasTrivialABIAttr(QualType VariableType)
Determines whether the VariableType's declaration is a record with the clang::trivial_abi attribute.
static void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned)
static bool buildCapturedStmtCaptureList(Sema &S, CapturedRegionScopeInfo *RSI, SmallVectorImpl< CapturedStmt::Capture > &Captures, SmallVectorImpl< Expr * > &CaptureInits)
static void checkCaseValue(Sema &S, SourceLocation Loc, const llvm::APSInt &Val, unsigned UnpromotedWidth, bool UnpromotedSign)
Check the specified case value is in range for the given unpromoted switch type.
static void CheckJumpOutOfSEHFinally(Sema &S, SourceLocation Loc, const Scope &DestScope)
Defines the Objective-C statement AST node classes.
enum clang::format::@1179::AnnotatingParser::Context::@327 ContextType
Defines the clang::TypeLoc interface and its subclasses.
Allows QualTypes to be sorted and hence used in maps and sets.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
unsigned getIntWidth(QualType T) const
bool hasSimilarType(QualType T1, QualType T2)
Determine if two types are similar, according to the C++ rules.
QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl, ObjCInterfaceDecl *PrevDecl=nullptr) const
getObjCInterfaceType - Return the unique reference to the type for the specified ObjC interface decl.
QualType getAutoRRefDeductType() const
C++11 deduction pattern for 'auto &&' type.
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl.
bool hasSameType(QualType T1, QualType T2) const
Determine whether the given types T1 and T2 are equivalent.
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
QualType getLValueReferenceType(QualType T, bool SpelledAsLValue=true) const
Return the uniqued reference to the type for an lvalue reference to the specified type.
QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl=nullptr) const
Return the unique reference to the type for the specified type declaration.
SelectorTable & Selectors
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
QualType getObjCObjectPointerType(QualType OIT) const
Return a ObjCObjectPointerType type for the given ObjCObjectType.
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
QualType getObjCIdType() const
Represents the Objective-CC id type.
bool hasSameUnqualifiedType(QualType T1, QualType T2) const
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
const TargetInfo & getTargetInfo() const
QualType getAutoDeductType() const
C++11 deduction pattern for 'auto' type.
void adjustDeducedFunctionResultType(FunctionDecl *FD, QualType ResultType)
Change the result type of a function type once it is deduced.
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Attr - This represents one attribute.
SourceLocation getLocation() const
SourceRange getRange() const