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"
42#include "llvm/ADT/StringExtras.h"
70 bool HasLeadingEmptyMacro) {
99 Diag(
decl->getLocation(), diag::err_non_variable_decl_in_for);
100 decl->setInvalidDecl();
133 enum { Equality, Inequality, Relational, ThreeWay } Kind;
136 if (!Op->isComparisonOp())
139 if (Op->getOpcode() == BO_EQ)
141 else if (Op->getOpcode() == BO_NE)
143 else if (Op->getOpcode() == BO_Cmp)
146 assert(Op->isRelationalOp());
149 Loc = Op->getOperatorLoc();
150 CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue();
152 switch (Op->getOperator()) {
156 case OO_ExclaimEqual:
161 case OO_GreaterEqual:
172 Loc = Op->getOperatorLoc();
173 CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue();
184 S.
Diag(Loc, diag::warn_unused_comparison)
190 if (Kind == Inequality)
191 S.
Diag(Loc, diag::note_inequality_comparison_to_or_assign)
193 else if (Kind == Equality)
194 S.
Diag(Loc, diag::note_equality_comparison_to_assign)
206 StringRef Msg = A->getMessage();
210 return S.
Diag(Loc, diag::warn_unused_constructor) << A << R1 << R2;
211 return S.
Diag(Loc, diag::warn_unused_result) << A << R1 << R2;
215 return S.
Diag(Loc, diag::warn_unused_constructor_msg) << A << Msg << R1
217 return S.
Diag(Loc, diag::warn_unused_result_msg) << A << Msg << R1 << R2;
224 const Expr *E = dyn_cast_or_null<Expr>(S);
239 bool ShouldSuppress =
243 const Expr *WarnExpr;
268 if (
const FullExpr *Temps = dyn_cast<FullExpr>(E))
269 E = Temps->getSubExpr();
271 E = TempExpr->getSubExpr();
277 if (
const auto *Cast = dyn_cast<CastExpr>(E))
278 if (Cast->getCastKind() == CK_NoOp ||
279 Cast->getCastKind() == CK_ConstructorConversion)
280 E = Cast->getSubExpr()->IgnoreImpCasts();
282 if (
const CallExpr *CE = dyn_cast<CallExpr>(E)) {
287 CE->getUnusedResultAttr(
Context)),
295 if (
const Decl *FD = CE->getCalleeDecl()) {
298 if (FD->hasAttr<PureAttr>()) {
299 Diag(Loc, diag::warn_unused_call) << R1 << R2 <<
"pure";
302 if (FD->hasAttr<ConstAttr>()) {
303 Diag(Loc, diag::warn_unused_call) << R1 << R2 <<
"const";
307 }
else if (
const auto *CE = dyn_cast<CXXConstructExpr>(E)) {
309 const auto *A = Ctor->getAttr<WarnUnusedResultAttr>();
310 A = A ? A : Ctor->getParent()->getAttr<WarnUnusedResultAttr>();
314 }
else if (
const auto *ILE = dyn_cast<InitListExpr>(E)) {
315 if (
const TagDecl *TD = ILE->getType()->getAsTagDecl()) {
321 }
else if (ShouldSuppress)
326 if (
getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) {
327 Diag(Loc, diag::err_arc_unused_init_message) << R1;
337 const Expr *Source = POE->getSyntacticForm();
339 if (
LangOpts.OpenMP && isa<CallExpr>(Source) &&
340 POE->getNumSemanticExprs() == 1 &&
341 isa<CallExpr>(POE->getSemanticExpr(0)))
343 if (isa<ObjCSubscriptRefExpr>(Source))
344 DiagID = diag::warn_unused_container_subscript_expr;
345 else if (isa<ObjCPropertyRefExpr>(Source))
346 DiagID = diag::warn_unused_property_expr;
348 = dyn_cast<CXXFunctionalCastExpr>(E)) {
349 const Expr *E = FC->getSubExpr();
351 E = TE->getSubExpr();
352 if (isa<CXXTemporaryObjectExpr>(E))
355 if (
const CXXRecordDecl *RD = CE->getType()->getAsCXXRecordDecl())
356 if (!RD->getAttr<WarnUnusedAttr>())
360 else if (
const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {
368 Diag(Loc, diag::warn_unused_voidptr)
378 Diag(Loc, diag::warn_unused_volatile) << R1 << R2;
385 if (DiagID != diag::warn_unused_comma_left_operand || !
isSFINAEContext())
387 PDiag(DiagID) << R1 << R2);
412 const unsigned NumElts = Elts.size();
417 const unsigned MixedDeclsCodeID =
getLangOpts().C99
418 ? diag::warn_mixed_decls_code
419 : diag::ext_mixed_decls_code;
424 for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
428 for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
432 Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
442 for (
unsigned i = 0; i != NumElts - 1; ++i)
478 auto CheckAndFinish = [&](
Expr *E) {
485 llvm::APSInt TempVal;
491 if (!E->isValueDependent())
505 if (Converted.
get() == Val.
get())
506 Converted = CheckAndFinish(Val.
get());
514 assert((LHSVal.
isInvalid() || LHSVal.
get()) &&
"missing LHS value");
517 "missing RHS value");
520 Diag(CaseLoc, diag::err_case_not_in_switch);
530 CaseLoc, DotDotDotLoc, ColonLoc);
537 cast<CaseStmt>(S)->setSubStmt(SubStmt);
544 Diag(DefaultLoc, diag::err_default_not_in_switch);
558 Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->
getDeclName();
566 Diag(IdentLoc, diag::warn_reserved_extern_symbol)
567 << TheDecl << static_cast<int>(Status);
588 for (
const auto *A : Attrs) {
589 if (A->getKind() == attr::MustTail) {
604 if (!SemanticAttrs.empty())
620 if (!checkMustTailAttr(St, MTA))
626 auto IgnoreImplicitAsWritten = [](
Expr *E) ->
Expr * {
637bool Sema::checkMustTailAttr(
const Stmt *St,
const Attr &MTA) {
639 "musttail cannot be checked from a dependent context");
642 auto IgnoreParenImplicitAsWritten = [](
const Expr *E) ->
const Expr * {
648 const Expr *E = cast<ReturnStmt>(St)->getRetValue();
649 const auto *CE = dyn_cast_or_null<CallExpr>(IgnoreParenImplicitAsWritten(E));
656 if (
const auto *EWC = dyn_cast<ExprWithCleanups>(E)) {
657 if (EWC->cleanupsHaveSideEffects()) {
658 Diag(St->
getBeginLoc(), diag::err_musttail_needs_trivial_args) << &MTA;
669 ft_non_static_member,
670 ft_pointer_to_member,
671 } MemberType = ft_non_member;
676 } CallerType, CalleeType;
679 bool IsCallee) ->
bool {
680 if (isa<CXXConstructorDecl, CXXDestructorDecl>(CMD)) {
682 << IsCallee << isa<CXXDestructorDecl>(CMD);
684 Diag(CMD->getBeginLoc(), diag::note_musttail_structors_forbidden)
685 << isa<CXXDestructorDecl>(CMD);
690 Type.MemberType = FuncType::ft_static_member;
692 Type.This = CMD->getThisObjectType();
693 Type.MemberType = FuncType::ft_non_static_member;
699 const auto *CallerDecl = dyn_cast<FunctionDecl>(
CurContext);
710 Diag(St->
getBeginLoc(), diag::err_musttail_forbidden_from_this_context)
713 }
else if (
const auto *CMD = dyn_cast<CXXMethodDecl>(
CurContext)) {
715 if (!GetMethodType(CMD, CallerType,
false))
723 const auto *CalleeBinOp = dyn_cast<BinaryOperator>(CalleeExpr);
725 ? CE->getCalleeDecl()->getBeginLoc()
730 dyn_cast_or_null<CXXMethodDecl>(CE->getCalleeDecl())) {
732 if (!GetMethodType(CMD, CalleeType,
true))
734 }
else if (CalleeBinOp && CalleeBinOp->isPtrMemOp()) {
738 CalleeType.This =
QualType(MPT->getClass(), 0);
740 CalleeType.MemberType = FuncType::ft_pointer_to_member;
741 }
else if (isa<CXXPseudoDestructorExpr>(CalleeExpr)) {
753 if (!CalleeType.Func || !CallerType.Func) {
755 if (!CalleeType.Func && CE->getDirectCallee()) {
756 Diag(CE->getDirectCallee()->getBeginLoc(),
757 diag::note_musttail_fix_non_prototype);
759 if (!CallerType.Func)
760 Diag(CallerDecl->getBeginLoc(), diag::note_musttail_fix_non_prototype);
771 if (CallerType.Func->getCallConv() != CalleeType.Func->getCallConv()) {
772 if (
const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl()))
774 <<
true << ND->getDeclName();
776 Diag(St->
getBeginLoc(), diag::err_musttail_callconv_mismatch) <<
false;
777 Diag(CalleeLoc, diag::note_musttail_callconv_mismatch)
784 if (CalleeType.Func->isVariadic() || CallerType.Func->isVariadic()) {
790 if (CallerType.This.isNull() != CalleeType.This.isNull()) {
791 if (
const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
793 << CallerType.MemberType << CalleeType.MemberType <<
true
794 << ND->getDeclName();
795 Diag(CalleeLoc, diag::note_musttail_callee_defined_here)
796 << ND->getDeclName();
799 << CallerType.MemberType << CalleeType.MemberType <<
false;
804 auto CheckTypesMatch = [
this](FuncType CallerType, FuncType CalleeType,
814 unsigned Select) ->
bool {
822 if (!CallerType.This.isNull() &&
826 if (!DoTypesMatch(CallerType.Func->getReturnType(),
830 if (CallerType.Func->getNumParams() != CalleeType.Func->getNumParams()) {
832 << CalleeType.Func->getNumParams();
838 size_t N = CallerType.Func->getNumParams();
839 for (
size_t I = 0; I < N; I++) {
840 if (!DoTypesMatch(CalleeParams[I], CallerParams[I],
842 PD << static_cast<int>(I) + 1;
851 if (!CheckTypesMatch(CallerType, CalleeType, PD)) {
852 if (
const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl()))
854 <<
true << ND->getDeclName();
870 CommaVisitor(
Sema &SemaRef) : Inherited(SemaRef.Context), SemaRef(SemaRef) {}
888 bool ConstevalOrNegatedConsteval =
892 Expr *CondExpr = Cond.
get().second;
893 assert((CondExpr || ConstevalOrNegatedConsteval) &&
894 "If statement: missing condition");
898 CommaVisitor(*this).Visit(CondExpr);
900 if (!ConstevalOrNegatedConsteval && !elseStmt)
903 if (ConstevalOrNegatedConsteval ||
905 auto DiagnoseLikelihood = [&](
const Stmt *S) {
908 diag::warn_attribute_has_no_effect_on_compile_time_if)
909 << A << ConstevalOrNegatedConsteval << A->getRange();
911 diag::note_attribute_has_no_effect_on_compile_time_if_here)
912 << ConstevalOrNegatedConsteval
919 DiagnoseLikelihood(thenStmt);
920 DiagnoseLikelihood(elseStmt);
922 std::tuple<bool, const Attr *, const Attr *> LHC =
924 if (std::get<0>(LHC)) {
925 const Attr *ThenAttr = std::get<1>(LHC);
926 const Attr *ElseAttr = std::get<2>(LHC);
928 diag::warn_attributes_likelihood_ifstmt_conflict)
929 << ThenAttr << ThenAttr->
getRange();
931 << ElseAttr << ElseAttr->
getRange();
935 if (ConstevalOrNegatedConsteval) {
941 if (FD && FD->isImmediateFunction())
945 Diags.
Report(IfLoc, diag::warn_consteval_if_always_true) << Immediate;
948 return BuildIfStmt(IfLoc, StatementKind, LParenLoc, InitStmt, Cond, RParenLoc,
949 thenStmt, ElseLoc, elseStmt);
962 isa<ObjCAvailabilityCheckExpr>(Cond.
get().second))
966 Cond.
get().first, Cond.
get().second, LParenLoc,
967 RParenLoc, thenStmt, ElseLoc, elseStmt);
971 struct CaseCompareFunctor {
972 bool operator()(
const std::pair<llvm::APSInt, CaseStmt*> &LHS,
973 const llvm::APSInt &RHS) {
974 return LHS.first < RHS;
976 bool operator()(
const std::pair<llvm::APSInt, CaseStmt*> &LHS,
977 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
978 return LHS.first < RHS.first;
980 bool operator()(
const llvm::APSInt &LHS,
981 const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
982 return LHS < RHS.first;
989static bool CmpCaseVals(
const std::pair<llvm::APSInt, CaseStmt*>& lhs,
990 const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
991 if (lhs.first < rhs.first)
994 if (lhs.first == rhs.first &&
995 lhs.second->getCaseLoc() < rhs.second->getCaseLoc())
1002static bool CmpEnumVals(
const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
1003 const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
1005 return lhs.first < rhs.first;
1010static bool EqEnumVals(
const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs,
1011 const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs)
1013 return lhs.first == rhs.first;
1019 if (
const auto *FE = dyn_cast<FullExpr>(E))
1020 E = FE->getSubExpr();
1021 while (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
1022 if (ImpCast->getCastKind() != CK_IntegralCast)
break;
1023 E = ImpCast->getSubExpr();
1033 SwitchConvertDiagnoser(
Expr *Cond)
1039 return S.
Diag(Loc, diag::err_typecheck_statement_requires_integer) << T;
1044 return S.
Diag(Loc, diag::err_switch_incomplete_class_type)
1050 return S.
Diag(Loc, diag::err_switch_explicit_conversion) << T << ConvTy;
1061 return S.
Diag(Loc, diag::err_switch_multiple_conversions) << T;
1072 llvm_unreachable(
"conversion functions are permitted");
1074 } SwitchDiagnoser(Cond);
1083 Cond = CondResult.
get();
1096 Expr *CondExpr = Cond.
get().second;
1097 assert((Cond.
isInvalid() || CondExpr) &&
"switch with no condition");
1110 Diag(SwitchLoc, diag::warn_bool_switch_condition)
1118 LParenLoc, RParenLoc);
1124static void AdjustAPSInt(llvm::APSInt &Val,
unsigned BitWidth,
bool IsSigned) {
1125 Val = Val.extOrTrunc(BitWidth);
1126 Val.setIsSigned(IsSigned);
1132 unsigned UnpromotedWidth,
bool UnpromotedSign) {
1140 if (UnpromotedWidth < Val.getBitWidth()) {
1141 llvm::APSInt ConvVal(Val);
1142 AdjustAPSInt(ConvVal, UnpromotedWidth, UnpromotedSign);
1143 AdjustAPSInt(ConvVal, Val.getBitWidth(), Val.isSigned());
1148 S.
Diag(Loc, diag::warn_case_value_overflow) <<
toString(Val, 10)
1159 const Expr *CaseExpr,
1160 EnumValsTy::iterator &EI,
1161 EnumValsTy::iterator &EIEnd,
1162 const llvm::APSInt &Val) {
1168 if (
const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
1177 if (ED->
hasAttr<FlagEnumAttr>())
1180 while (EI != EIEnd && EI->first < Val)
1183 if (EI != EIEnd && EI->first == Val)
1196 if (!CondEnumType || !CaseEnumType)
1203 if (!CaseEnumType->getDecl()->getIdentifier() &&
1204 !CaseEnumType->getDecl()->getTypedefNameForAnonDecl())
1210 S.
Diag(Case->
getExprLoc(), diag::warn_comparison_of_mixed_enum_types_switch)
1220 assert(SS ==
getCurFunction()->SwitchStack.back().getPointer() &&
1221 "switch stack missing push/pop!");
1226 SS->
setBody(BodyStmt, SwitchLoc);
1240 const Expr *CondExprBeforePromotion = CondExpr;
1246 bool HasDependentValue
1255 unsigned CondWidthBeforePromotion
1257 bool CondIsSignedBeforePromotion
1264 CaseValsTy CaseVals;
1267 typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy;
1268 CaseRangesTy CaseRanges;
1272 bool CaseListIsErroneous =
false;
1277 if (
DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
1278 if (TheDefaultStmt) {
1279 Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
1286 CaseListIsErroneous =
true;
1288 TheDefaultStmt = DS;
1296 HasDependentValue =
true;
1302 const Expr *LoBeforePromotion = Lo;
1309 CondIsSignedBeforePromotion);
1321 HasDependentValue =
true;
1324 CaseRanges.push_back(std::make_pair(LoVal, CS));
1326 CaseVals.push_back(std::make_pair(LoVal, CS));
1330 if (!HasDependentValue) {
1333 llvm::APSInt ConstantCondValue;
1334 bool HasConstantCond =
false;
1335 if (!TheDefaultStmt) {
1340 ConstantCondValue =
Result.Val.getInt();
1341 assert(!HasConstantCond ||
1342 (ConstantCondValue.getBitWidth() == CondWidth &&
1343 ConstantCondValue.isSigned() == CondIsSigned));
1345 bool ShouldCheckConstantCond = HasConstantCond;
1350 if (!CaseVals.empty()) {
1351 for (
unsigned i = 0, e = CaseVals.size(); i != e; ++i) {
1352 if (ShouldCheckConstantCond &&
1353 CaseVals[i].first == ConstantCondValue)
1354 ShouldCheckConstantCond =
false;
1356 if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) {
1359 StringRef PrevString, CurrString;
1362 if (
DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(PrevCase)) {
1363 PrevString = DeclRef->getDecl()->getName();
1365 if (
DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(CurrCase)) {
1366 CurrString = DeclRef->getDecl()->getName();
1369 CaseVals[i-1].first.toString(CaseValStr);
1371 if (PrevString == CurrString)
1372 Diag(CaseVals[i].second->getLHS()->getBeginLoc(),
1373 diag::err_duplicate_case)
1374 << (PrevString.empty() ? CaseValStr.str() : PrevString);
1376 Diag(CaseVals[i].second->getLHS()->getBeginLoc(),
1377 diag::err_duplicate_case_differing_expr)
1378 << (PrevString.empty() ? CaseValStr.str() : PrevString)
1379 << (CurrString.empty() ? CaseValStr.str() : CurrString)
1382 Diag(CaseVals[i - 1].second->getLHS()->getBeginLoc(),
1383 diag::note_duplicate_case_prev);
1386 CaseListIsErroneous =
true;
1393 if (!CaseRanges.empty()) {
1396 llvm::stable_sort(CaseRanges);
1399 std::vector<llvm::APSInt> HiVals;
1400 for (
unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
1401 llvm::APSInt &LoVal = CaseRanges[i].first;
1402 CaseStmt *CR = CaseRanges[i].second;
1405 const Expr *HiBeforePromotion = Hi;
1412 CondWidthBeforePromotion, CondIsSignedBeforePromotion);
1418 if (LoVal > HiVal) {
1421 CaseRanges.erase(CaseRanges.begin()+i);
1427 if (ShouldCheckConstantCond &&
1428 LoVal <= ConstantCondValue &&
1429 ConstantCondValue <= HiVal)
1430 ShouldCheckConstantCond =
false;
1432 HiVals.push_back(HiVal);
1438 for (
unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
1439 llvm::APSInt &CRLo = CaseRanges[i].first;
1440 llvm::APSInt &CRHi = HiVals[i];
1441 CaseStmt *CR = CaseRanges[i].second;
1446 llvm::APSInt OverlapVal(32);
1450 CaseValsTy::iterator I =
1451 llvm::lower_bound(CaseVals, CRLo, CaseCompareFunctor());
1452 if (I != CaseVals.end() && I->first < CRHi) {
1453 OverlapVal = I->first;
1454 OverlapStmt = I->second;
1458 I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
1459 if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
1460 OverlapVal = (I-1)->first;
1461 OverlapStmt = (I-1)->second;
1466 if (i && CRLo <= HiVals[i-1]) {
1467 OverlapVal = HiVals[i-1];
1468 OverlapStmt = CaseRanges[i-1].second;
1476 diag::note_duplicate_case_prev);
1479 CaseListIsErroneous =
true;
1485 if (!CaseListIsErroneous && !CaseListIsIncomplete &&
1486 ShouldCheckConstantCond) {
1489 Diag(CondExpr->
getExprLoc(), diag::warn_missing_case_for_condition)
1502 if (!CaseListIsErroneous && !CaseListIsIncomplete && !HasConstantCond &&
1511 llvm::APSInt Val = EDI->getInitVal();
1513 EnumVals.push_back(std::make_pair(Val, EDI));
1516 auto EI = EnumVals.begin(), EIEnd =
1517 std::unique(EnumVals.begin(), EnumVals.end(),
EqEnumVals);
1520 for (CaseValsTy::const_iterator CI = CaseVals.begin();
1521 CI != CaseVals.end(); CI++) {
1522 Expr *CaseExpr = CI->second->getLHS();
1526 << CondTypeBeforePromotion;
1530 EI = EnumVals.begin();
1531 for (CaseRangesTy::const_iterator RI = CaseRanges.begin();
1532 RI != CaseRanges.end(); RI++) {
1533 Expr *CaseExpr = RI->second->getLHS();
1537 << CondTypeBeforePromotion;
1540 RI->second->getRHS()->EvaluateKnownConstInt(
Context);
1543 CaseExpr = RI->second->getRHS();
1547 << CondTypeBeforePromotion;
1551 auto CI = CaseVals.begin();
1552 auto RI = CaseRanges.begin();
1553 bool hasCasesNotInSwitch =
false;
1557 for (EI = EnumVals.begin(); EI != EIEnd; EI++) {
1559 switch (EI->second->getAvailability()) {
1572 if (EI->second->hasAttr<UnusedAttr>())
1576 while (CI != CaseVals.end() && CI->first < EI->first)
1579 if (CI != CaseVals.end() && CI->first == EI->first)
1583 for (; RI != CaseRanges.end(); RI++) {
1585 RI->second->getRHS()->EvaluateKnownConstInt(
Context);
1587 if (EI->first <= Hi)
1591 if (RI == CaseRanges.end() || EI->first < RI->first) {
1592 hasCasesNotInSwitch =
true;
1593 UnhandledNames.push_back(EI->second->getDeclName());
1597 if (TheDefaultStmt && UnhandledNames.empty() && ED->
isClosedNonFlag())
1601 if (!UnhandledNames.empty()) {
1603 ? diag::warn_def_missing_case
1604 : diag::warn_missing_case)
1607 for (
size_t I = 0, E = std::min(UnhandledNames.size(), (
size_t)3);
1609 DB << UnhandledNames[I];
1612 if (!hasCasesNotInSwitch)
1619 diag::warn_empty_switch_body);
1623 if (CaseListIsErroneous)
1646 const EnumDecl *ED = ET->getDecl();
1651 if (ED->
hasAttr<FlagEnumAttr>()) {
1663 llvm::APSInt Val = EDI->getInitVal();
1665 EnumVals.push_back(std::make_pair(Val, EDI));
1667 if (EnumVals.empty())
1670 EnumValsTy::iterator EIend =
1671 std::unique(EnumVals.begin(), EnumVals.end(),
EqEnumVals);
1674 EnumValsTy::const_iterator EI = EnumVals.begin();
1675 while (EI != EIend && EI->first < RhsVal)
1677 if (EI == EIend || EI->first != RhsVal) {
1692 auto CondVal = Cond.
get();
1693 CheckBreakContinueBinding(CondVal.second);
1695 if (CondVal.second &&
1696 !
Diags.
isIgnored(diag::warn_comma_operator, CondVal.second->getExprLoc()))
1697 CommaVisitor(*this).Visit(CondVal.second);
1699 if (isa<NullStmt>(Body))
1703 WhileLoc, LParenLoc, RParenLoc);
1710 assert(Cond &&
"ActOnDoStmt(): missing expression");
1712 CheckBreakContinueBinding(Cond);
1716 Cond = CondResult.
get();
1721 Cond = CondResult.
get();
1726 CommaVisitor(*this).Visit(Cond);
1728 return new (
Context)
DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen);
1739 DeclSetVector &Decls;
1745 DeclExtractor(
Sema &S, DeclSetVector &Decls,
1747 Inherited(S.Context),
1752 bool isSimple() {
return Simple; }
1761 void VisitStmt(
Stmt *S) { Simple =
false; }
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) {
3582 bool HasDeducedReturnType =
3592 RetValExp = ER.
get();
3598 if (HasDeducedReturnType) {
3611 assert(AT &&
"lost auto type from lambda return type");
3623 if (RetValExp && !isa<InitListExpr>(RetValExp)) {
3627 RetValExp =
Result.get();
3642 Diag(ReturnLoc, diag::err_lambda_return_init_list)
3656 if (
auto *CurBlock = dyn_cast<BlockScopeInfo>(CurCap)) {
3658 Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr);
3661 }
else if (
auto *CurRegion = dyn_cast<CapturedRegionScopeInfo>(CurCap)) {
3662 Diag(ReturnLoc, diag::err_return_in_captured_stmt) << CurRegion->getRegionName();
3665 assert(CurLambda &&
"unknown kind of captured scope");
3669 Diag(ReturnLoc, diag::err_noreturn_lambda_has_return_expr);
3681 if (RetValExp && !isa<InitListExpr>(RetValExp) &&
3687 Diag(ReturnLoc, diag::ext_return_has_void_expr) <<
"literal" << 2;
3689 Diag(ReturnLoc, diag::err_return_block_has_expr);
3690 RetValExp =
nullptr;
3693 }
else if (!RetValExp) {
3694 return StmtError(
Diag(ReturnLoc, diag::err_block_return_missing_expr));
3707 Entity, NRInfo, RetValExp, SupressSimplerImplicitMoves);
3712 RetValExp = Res.
get();
3713 CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc);
3721 RetValExp = ER.
get();
3735 if (
auto *CurBlock = dyn_cast<BlockScopeInfo>(CurCap);
3738 CurBlock->TheDecl->setInvalidDecl();
3756class LocalTypedefNameReferencer
3759 LocalTypedefNameReferencer(
Sema &S) : S(S) {}
3764bool LocalTypedefNameReferencer::VisitRecordType(
const RecordType *RT) {
3765 auto *R = dyn_cast<CXXRecordDecl>(RT->
getDecl());
3766 if (!R || !R->isLocalClass() || !R->isLocalClass()->isExternallyVisible() ||
3767 R->isDependentType())
3769 for (
auto *TmpD : R->decls())
3770 if (
auto *T = dyn_cast<TypedefNameDecl>(TmpD))
3771 if (T->getAccess() !=
AS_private || R->hasFriends())
3795 if (RetExpr && isa<InitListExpr>(RetExpr)) {
3800 : diag::err_auto_fn_return_init_list)
3810 assert(AT->
isDeduced() &&
"should have deduced to dependent type");
3824 Diag(ReturnLoc, diag::err_auto_fn_return_void_but_not_auto)
3840 if (FindResult.Expression)
3841 TemplateSpecLoc = FindResult.Expression->getNameLoc();
3845 OrigResultType, RetExpr, Deduced, Info,
false,
3861 Diag(ReturnLoc, diag::err_typecheck_missing_return_type_incompatible)
3864 Diag(ReturnLoc, diag::err_auto_fn_different_deductions)
3879 LocalTypedefNameReferencer(*this).TraverseType(RetExpr->
getType());
3902 RetValExp,
nullptr,
true);
3911 const_cast<VarDecl *
>(cast<ReturnStmt>(R.
get())->getNRVOCandidate());
3935 bool AllowRecovery) {
3943 bool SupressSimplerImplicitMoves =
3951 SupressSimplerImplicitMoves);
3955 const AttrVec *Attrs =
nullptr;
3956 bool isObjCMethod =
false;
3963 Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) << FD;
3964 if (FD->
isMain() && RetValExp)
3965 if (isa<CXXBoolLiteralExpr>(RetValExp))
3966 Diag(ReturnLoc, diag::warn_main_returns_bool_literal)
3968 if (FD->
hasAttr<CmseNSEntryAttr>() && RetValExp) {
3975 FnRetType = MD->getReturnType();
3976 isObjCMethod =
true;
3978 Attrs = &MD->getAttrs();
3979 if (MD->hasRelatedResultType() && MD->getClassInterface()) {
3990 const auto *ATy = dyn_cast<ArrayType>(RetValExp->
getType());
3991 if (ATy && ATy->getElementType().isWebAssemblyReferenceType()) {
3992 Diag(ReturnLoc, diag::err_wasm_table_art) << 1;
4006 RetValExp = ER.
get();
4032 AT->isDeduced() ? FnRetType :
QualType());
4033 if (Recovery.isInvalid())
4035 RetValExp = Recovery.get();
4051 if (
auto *ILE = dyn_cast<InitListExpr>(RetValExp)) {
4056 int FunctionKind = 0;
4057 if (isa<ObjCMethodDecl>(CurDecl))
4059 else if (isa<CXXConstructorDecl>(CurDecl))
4061 else if (isa<CXXDestructorDecl>(CurDecl))
4064 Diag(ReturnLoc, diag::err_return_init_list)
4068 RetValExp = AllowRecovery
4070 ILE->getRBraceLoc(), ILE->inits())
4075 unsigned D = diag::ext_return_has_expr;
4078 if (isa<CXXConstructorDecl>(CurDecl) ||
4079 isa<CXXDestructorDecl>(CurDecl))
4080 D = diag::err_ctor_dtor_returns_void;
4082 D = diag::ext_return_has_void_expr;
4089 RetValExp =
Result.get();
4094 if (D == diag::err_ctor_dtor_returns_void) {
4096 Diag(ReturnLoc, D) << CurDecl << isa<CXXDestructorDecl>(CurDecl)
4100 else if (D != diag::ext_return_has_void_expr ||
4104 int FunctionKind = 0;
4105 if (isa<ObjCMethodDecl>(CurDecl))
4107 else if (isa<CXXConstructorDecl>(CurDecl))
4109 else if (isa<CXXDestructorDecl>(CurDecl))
4122 RetValExp = ER.
get();
4128 }
else if (!RetValExp && !HasDependentReturnType) {
4135 Diag(ReturnLoc, diag::err_constexpr_return_missing_expr)
4141 unsigned DiagID =
getLangOpts().C99 ? diag::ext_return_missing_expr
4142 : diag::warn_return_missing_expr;
4146 "Not in a FunctionDecl or ObjCMethodDecl?");
4147 bool IsMethod = FD ==
nullptr;
4150 Diag(ReturnLoc, DiagID) << ND << IsMethod;
4156 assert(RetValExp || HasDependentReturnType);
4157 QualType RetType = RelatedRetType.
isNull() ? FnRetType : RelatedRetType;
4170 Entity, NRInfo, RetValExp, SupressSimplerImplicitMoves);
4173 RetValExp->
getEndLoc(), RetValExp, RetType);
4184 if (!RelatedRetType.
isNull()) {
4195 CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc, isObjCMethod, Attrs,
4204 RetValExp = ER.
get();
4211 if (
Result->getNRVOCandidate())
4224 VarDecl *Var = cast_or_null<VarDecl>(Parm);
4240 Diag(AtLoc, diag::err_objc_exceptions_disabled) <<
"@try";
4245 Diag(AtLoc, diag::err_mixing_cxx_try_seh_try) << 1;
4250 unsigned NumCatchStmts = CatchStmts.size();
4252 NumCatchStmts, Finally);
4272 return StmtError(
Diag(AtLoc, diag::err_objc_throw_expects_object)
4284 Diag(AtLoc, diag::err_objc_exceptions_disabled) <<
"@throw";
4289 Scope *AtCatchParent = CurScope;
4291 AtCatchParent = AtCatchParent->
getParent();
4293 return StmtError(
Diag(AtLoc, diag::err_rethrow_used_outside_catch));
4303 operand = result.
get();
4307 if (!
type->isDependentType() &&
4308 !
type->isObjCObjectPointerType()) {
4313 diag::err_incomplete_receiver_type))
4314 return Diag(atLoc, diag::err_objc_synchronized_expects_object)
4321 return Diag(atLoc, diag::err_objc_synchronized_expects_object)
4324 operand = result.
get();
4326 return Diag(atLoc, diag::err_objc_synchronized_expects_object)
4348 Stmt *HandlerBlock) {
4351 CXXCatchStmt(CatchLoc, cast_or_null<VarDecl>(ExDecl), HandlerBlock);
4361class CatchHandlerType {
4363 unsigned IsPointer : 1;
4367 friend struct llvm::DenseMapInfo<CatchHandlerType>;
4368 enum Unique { ForDenseMap };
4369 CatchHandlerType(
QualType QT, Unique) : QT(QT), IsPointer(
false) {}
4387 CatchHandlerType(
QualType QT,
bool IsPointer)
4388 : QT(QT), IsPointer(IsPointer) {}
4390 QualType underlying()
const {
return QT; }
4391 bool isPointer()
const {
return IsPointer; }
4393 friend bool operator==(
const CatchHandlerType &LHS,
4394 const CatchHandlerType &RHS) {
4396 if (LHS.IsPointer != RHS.IsPointer)
4399 return LHS.QT == RHS.QT;
4405template <>
struct DenseMapInfo<CatchHandlerType> {
4407 return CatchHandlerType(DenseMapInfo<QualType>::getEmptyKey(),
4408 CatchHandlerType::ForDenseMap);
4412 return CatchHandlerType(DenseMapInfo<QualType>::getTombstoneKey(),
4413 CatchHandlerType::ForDenseMap);
4417 return DenseMapInfo<QualType>::getHashValue(
Base.underlying());
4421 const CatchHandlerType &RHS) {
4428class CatchTypePublicBases {
4429 const llvm::DenseMap<QualType, CXXCatchStmt *> &TypesToCheck;
4436 CatchTypePublicBases(
const llvm::DenseMap<QualType, CXXCatchStmt *> &T,
4438 : TypesToCheck(T), FoundHandler(nullptr), TestAgainstType(QT) {}
4440 CXXCatchStmt *getFoundHandler()
const {
return FoundHandler; }
4441 QualType getFoundHandlerType()
const {
return FoundHandlerType; }
4444 if (S->getAccessSpecifier() == AccessSpecifier::AS_public) {
4445 QualType Check = S->getType().getCanonicalType();
4446 const auto &M = TypesToCheck;
4447 auto I = M.find(Check);
4459 if (I->second->getCaughtType()->isPointerType() ==
4461 FoundHandler = I->second;
4462 FoundHandlerType = Check;
4477 const bool IsOpenMPGPUTarget =
4478 getLangOpts().OpenMPIsTargetDevice && (T.isNVPTX() || T.isAMDGCN());
4481 if (!IsOpenMPGPUTarget && !
getLangOpts().CXXExceptions &&
4484 targetDiag(TryLoc, diag::err_exceptions_disabled) <<
"try";
4489 if (IsOpenMPGPUTarget)
4490 targetDiag(TryLoc, diag::warn_try_not_valid_on_target) << T.str();
4498 Diag(TryLoc, diag::err_omp_simd_region_cannot_use_stmt) <<
"try";
4504 Diag(TryLoc, diag::err_mixing_cxx_try_seh_try) << 0;
4508 const unsigned NumHandlers = Handlers.size();
4509 assert(!Handlers.empty() &&
4510 "The parser shouldn't call this if there are no handlers.");
4512 llvm::DenseMap<QualType, CXXCatchStmt *> HandledBaseTypes;
4513 llvm::DenseMap<CatchHandlerType, CXXCatchStmt *> HandledTypes;
4514 for (
unsigned i = 0; i < NumHandlers; ++i) {
4521 if (i < NumHandlers - 1)
4535 QualType Underlying = HandlerCHT.underlying();
4537 if (!RD->hasDefinition())
4545 Paths.setOrigin(RD);
4546 CatchTypePublicBases CTPB(HandledBaseTypes,
4548 if (RD->lookupInBases(CTPB, Paths)) {
4550 if (!Paths.isAmbiguous(
4553 diag::warn_exception_caught_by_earlier_handler)
4556 diag::note_previous_exception_handler)
4568 auto R = HandledTypes.insert(
4573 diag::warn_exception_caught_by_earlier_handler)
4576 diag::note_previous_exception_handler)
4589 assert(TryBlock && Handler);
4616 Diag(TryLoc, diag::err_seh_try_outside_functions);
4620 Diag(TryLoc, diag::err_seh_try_unsupported);
4627 assert(FilterExpr &&
Block);
4631 Diag(FilterExpr->
getExprLoc(), diag::err_filter_expression_integral)
4653 Scope *SEHTryParent = CurScope;
4655 SEHTryParent = SEHTryParent->
getParent();
4657 return StmtError(
Diag(Loc, diag::err_ms___leave_not_in___try));
4670 QualifierLoc, NameInfo,
4671 cast<CompoundStmt>(Nested));
4688 unsigned NumParams) {
4705 assert(NumParams > 0 &&
"CapturedStmt requires context parameter");
4748 CaptureInits.push_back(Init.get());
4755 unsigned NumParams) {
4786 unsigned OpenMPCaptureLevel) {
4792 bool ContextIsFound =
false;
4793 unsigned ParamNum = 0;
4796 I != E; ++I, ++ParamNum) {
4797 if (I->second.isNull()) {
4798 assert(!ContextIsFound &&
4799 "null type has been found already for '__context' parameter");
4809 ContextIsFound =
true;
4819 assert(ContextIsFound &&
"no null type for '__context' parameter");
4820 if (!ContextIsFound) {
4850 Record->setInvalidDecl();
4853 ActOnFields(
nullptr, Record->getLocation(), Record, Fields,
4876 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::@1210::AnnotatingParser::Context::@329 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 estim