@@ -10377,11 +10377,6 @@ void InitializationSequence::dump() const {
10377
10377
dump (llvm::errs ());
10378
10378
}
10379
10379
10380
- static bool NarrowingErrs (const LangOptions &L) {
10381
- return L.CPlusPlus11 &&
10382
- (!L.MicrosoftExt || L.isCompatibleWithMSVC (LangOptions::MSVC2015));
10383
- }
10384
-
10385
10380
static void DiagnoseNarrowingInInitList (Sema &S,
10386
10381
const ImplicitConversionSequence &ICS,
10387
10382
QualType PreNarrowingType,
@@ -10402,6 +10397,19 @@ static void DiagnoseNarrowingInInitList(Sema &S,
10402
10397
return ;
10403
10398
}
10404
10399
10400
+ auto MakeDiag = [&](bool IsConstRef, unsigned DefaultDiagID,
10401
+ unsigned ConstRefDiagID, unsigned WarnDiagID) {
10402
+ unsigned DiagID;
10403
+ auto &L = S.getLangOpts ();
10404
+ if (L.CPlusPlus11 &&
10405
+ (!L.MicrosoftExt || L.isCompatibleWithMSVC (LangOptions::MSVC2015)))
10406
+ DiagID = IsConstRef ? ConstRefDiagID : DefaultDiagID;
10407
+ else
10408
+ DiagID = WarnDiagID;
10409
+ return S.Diag (PostInit->getBeginLoc (), DiagID)
10410
+ << PostInit->getSourceRange ();
10411
+ };
10412
+
10405
10413
// C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion.
10406
10414
APValue ConstantValue;
10407
10415
QualType ConstantType;
@@ -10417,43 +10425,31 @@ static void DiagnoseNarrowingInInitList(Sema &S,
10417
10425
// narrowing conversion even if the value is a constant and can be
10418
10426
// represented exactly as an integer.
10419
10427
QualType T = EntityType.getNonReferenceType ();
10420
- S.Diag (PostInit->getBeginLoc (),
10421
- NarrowingErrs (S.getLangOpts ())
10422
- ? (T == EntityType
10423
- ? diag::ext_init_list_type_narrowing
10424
- : diag::ext_init_list_type_narrowing_const_reference)
10425
- : diag::warn_init_list_type_narrowing)
10426
- << PostInit->getSourceRange ()
10428
+ MakeDiag (T != EntityType, diag::ext_init_list_type_narrowing,
10429
+ diag::ext_init_list_type_narrowing_const_reference,
10430
+ diag::warn_init_list_type_narrowing)
10427
10431
<< PreNarrowingType.getLocalUnqualifiedType ()
10428
10432
<< T.getLocalUnqualifiedType ();
10429
10433
break ;
10430
10434
}
10431
10435
10432
10436
case NK_Constant_Narrowing: {
10433
10437
// A constant value was narrowed.
10434
- QualType T = EntityType.getNonReferenceType ();
10435
- S.Diag (PostInit->getBeginLoc (),
10436
- NarrowingErrs (S.getLangOpts ())
10437
- ? (T == EntityType
10438
- ? diag::ext_init_list_constant_narrowing
10439
- : diag::ext_init_list_constant_narrowing_const_reference)
10440
- : diag::warn_init_list_constant_narrowing)
10441
- << PostInit->getSourceRange ()
10438
+ MakeDiag (EntityType.getNonReferenceType () != EntityType,
10439
+ diag::ext_init_list_constant_narrowing,
10440
+ diag::ext_init_list_constant_narrowing_const_reference,
10441
+ diag::warn_init_list_constant_narrowing)
10442
10442
<< ConstantValue.getAsString (S.getASTContext (), ConstantType)
10443
10443
<< EntityType.getNonReferenceType ().getLocalUnqualifiedType ();
10444
10444
break ;
10445
10445
}
10446
10446
10447
10447
case NK_Variable_Narrowing: {
10448
10448
// A variable's value may have been narrowed.
10449
- QualType T = EntityType.getNonReferenceType ();
10450
- S.Diag (PostInit->getBeginLoc (),
10451
- NarrowingErrs (S.getLangOpts ())
10452
- ? (T == EntityType
10453
- ? diag::ext_init_list_variable_narrowing
10454
- : diag::ext_init_list_variable_narrowing_const_reference)
10455
- : diag::warn_init_list_variable_narrowing)
10456
- << PostInit->getSourceRange ()
10449
+ MakeDiag (EntityType.getNonReferenceType () != EntityType,
10450
+ diag::ext_init_list_variable_narrowing,
10451
+ diag::ext_init_list_variable_narrowing_const_reference,
10452
+ diag::warn_init_list_variable_narrowing)
10457
10453
<< PreNarrowingType.getLocalUnqualifiedType ()
10458
10454
<< EntityType.getNonReferenceType ().getLocalUnqualifiedType ();
10459
10455
break ;
0 commit comments