Kaydet (Commit) 7f3ca309 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Enable -Wunreachable-code

...motivated by <https://gerrit.libreoffice.org/#/c/41565/2> adding dead code
at the end of a switch statement, after the last case's "break".

-Wunreachable-code appears to work well on Clang, while it appears to have no
effect on GCC.

Most of the affected places are apparently temporary/TODO/FIXME cases of
disabling code via "if (false)", which can be written with an extra set of
parentheses as "if ((false))" to silence -Wunreachable-code on Clang (which thus
needed loplugin:unnecessaryparen to be adapted accordingly).  In some cases,
the controlling expression was more complex than just "false" and needed to be
rewritten by taking it out of the if statement to silence Clang.

One noteworthy case where the nature of the disabled code wasn't immediately
apparent:

  Sep 12 16:59:58 <sberg> quikee, is that "if (false)" in
   ScExponentialSmoothingDialog::ApplyOutput
   (sc/source/ui/StatisticsDialogs/ExponentialSmoothingDialog.cxx) some work-in-
   progress or dead code?
  Sep 12 17:02:03 <quikee> sberg: WIP, but you can remove it
  Sep 12 17:04:47 <sberg> quikee, I'll wrap the false in an extra set of
   parentheses for now, to silence -Wunreachable-code (I wouldn't want to
   remove it, as I have no idea whether I should then also remove the "Initial
   value" comment preceding it)
  Sep 12 17:07:29 <quikee> sberg: both are different ways to calculate the
   "intital value"... so no

Another case where the nature of the dead code, following while (true) loops
without breaks, is unclear is sd/source/ui/remotecontrol/BluetoothServer.cxx,
where I added TODO markers to the workarounds that silence the warnings for now.

basic/source/sbx/sbxvalue.cxx had a variable of type double, of automatic
storage duration, and without an initalizer at the top of a switch statement.
Clang warning about it is arguably a false positive.

Apart from that, this didn't find any cases of genuinely dead code in the
existing code base.

Change-Id: Ib00b822c8efec94278c048783d5997b8ba86a94c
Reviewed-on: https://gerrit.libreoffice.org/42217Tested-by: 's avatarStephan Bergmann <sbergman@redhat.com>
Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst 1963dc64
...@@ -1062,73 +1062,80 @@ bool SbxValue::Compute( SbxOperator eOp, const SbxValue& rOp ) ...@@ -1062,73 +1062,80 @@ bool SbxValue::Compute( SbxOperator eOp, const SbxValue& rOp )
if( Get( aL ) ) switch( eOp ) if( Get( aL ) ) switch( eOp )
{ {
double dTest;
case SbxMUL: case SbxMUL:
// first overflow check: see if product will fit - test real value of product (hence 2 curr factors)
dTest = (double)aL.nInt64 * (double)aR.nInt64 / (double)CURRENCY_FACTOR_SQUARE;
if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
{ {
aL.nInt64 = SAL_MAX_INT64; // first overflow check: see if product will fit - test real value of product (hence 2 curr factors)
if( dTest < SbxMINCURR ) aL.nInt64 = SAL_MIN_INT64; double dTest = (double)aL.nInt64 * (double)aR.nInt64 / (double)CURRENCY_FACTOR_SQUARE;
SetError( ERRCODE_BASIC_MATH_OVERFLOW ); if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
break; {
} aL.nInt64 = SAL_MAX_INT64;
// second overflow check: see if unscaled product overflows - if so use doubles if( dTest < SbxMINCURR ) aL.nInt64 = SAL_MIN_INT64;
dTest = (double)aL.nInt64 * (double)aR.nInt64; SetError( ERRCODE_BASIC_MATH_OVERFLOW );
if( dTest < SAL_MIN_INT64 || SAL_MAX_INT64 < dTest) break;
{ }
aL.nInt64 = (sal_Int64)( dTest / (double)CURRENCY_FACTOR ); // second overflow check: see if unscaled product overflows - if so use doubles
dTest = (double)aL.nInt64 * (double)aR.nInt64;
if( dTest < SAL_MIN_INT64 || SAL_MAX_INT64 < dTest)
{
aL.nInt64 = (sal_Int64)( dTest / (double)CURRENCY_FACTOR );
break;
}
// precise calc: multiply then scale back (move decimal pt)
aL.nInt64 *= aR.nInt64;
aL.nInt64 /= CURRENCY_FACTOR;
break; break;
} }
// precise calc: multiply then scale back (move decimal pt)
aL.nInt64 *= aR.nInt64;
aL.nInt64 /= CURRENCY_FACTOR;
break;
case SbxDIV: case SbxDIV:
if( !aR.nInt64 )
{ {
SetError( ERRCODE_BASIC_ZERODIV ); if( !aR.nInt64 )
break; {
} SetError( ERRCODE_BASIC_ZERODIV );
// first overflow check: see if quotient will fit - calc real value of quotient (curr factors cancel) break;
dTest = (double)aL.nInt64 / (double)aR.nInt64; }
if( dTest < SbxMINCURR || SbxMAXCURR < dTest) // first overflow check: see if quotient will fit - calc real value of quotient (curr factors cancel)
{ double dTest = (double)aL.nInt64 / (double)aR.nInt64;
SetError( ERRCODE_BASIC_MATH_OVERFLOW ); if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
break; {
} SetError( ERRCODE_BASIC_MATH_OVERFLOW );
// second overflow check: see if scaled dividend overflows - if so use doubles break;
dTest = (double)aL.nInt64 * (double)CURRENCY_FACTOR; }
if( dTest < SAL_MIN_INT64 || SAL_MAX_INT64 < dTest) // second overflow check: see if scaled dividend overflows - if so use doubles
{ dTest = (double)aL.nInt64 * (double)CURRENCY_FACTOR;
aL.nInt64 = (sal_Int64)(dTest / (double)aR.nInt64); if( dTest < SAL_MIN_INT64 || SAL_MAX_INT64 < dTest)
{
aL.nInt64 = (sal_Int64)(dTest / (double)aR.nInt64);
break;
}
// precise calc: scale (move decimal pt) then divide
aL.nInt64 *= CURRENCY_FACTOR;
aL.nInt64 /= aR.nInt64;
break; break;
} }
// precise calc: scale (move decimal pt) then divide
aL.nInt64 *= CURRENCY_FACTOR;
aL.nInt64 /= aR.nInt64;
break;
case SbxPLUS: case SbxPLUS:
dTest = ( (double)aL.nInt64 + (double)aR.nInt64 ) / (double)CURRENCY_FACTOR;
if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
{ {
SetError( ERRCODE_BASIC_MATH_OVERFLOW ); double dTest = ( (double)aL.nInt64 + (double)aR.nInt64 ) / (double)CURRENCY_FACTOR;
if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
{
SetError( ERRCODE_BASIC_MATH_OVERFLOW );
break;
}
aL.nInt64 += aR.nInt64;
break; break;
} }
aL.nInt64 += aR.nInt64;
break;
case SbxMINUS: case SbxMINUS:
dTest = ( (double)aL.nInt64 - (double)aR.nInt64 ) / (double)CURRENCY_FACTOR;
if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
{ {
SetError( ERRCODE_BASIC_MATH_OVERFLOW ); double dTest = ( (double)aL.nInt64 - (double)aR.nInt64 ) / (double)CURRENCY_FACTOR;
if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
{
SetError( ERRCODE_BASIC_MATH_OVERFLOW );
break;
}
aL.nInt64 -= aR.nInt64;
break; break;
} }
aL.nInt64 -= aR.nInt64;
break;
case SbxNEG: case SbxNEG:
aL.nInt64 = -aL.nInt64; aL.nInt64 = -aL.nInt64;
break; break;
......
...@@ -82,7 +82,7 @@ void PropertyMapper::getValueMap( ...@@ -82,7 +82,7 @@ void PropertyMapper::getValueMap(
tPropertyNameMap::const_iterator aEnd( rNameMap.end() ); tPropertyNameMap::const_iterator aEnd( rNameMap.end() );
uno::Reference< beans::XMultiPropertySet > xMultiPropSet(xSourceProp, uno::UNO_QUERY); uno::Reference< beans::XMultiPropertySet > xMultiPropSet(xSourceProp, uno::UNO_QUERY);
if(false && xMultiPropSet.is()) if((false) && xMultiPropSet.is())
{ {
uno::Sequence< rtl::OUString > aPropSourceNames(rNameMap.size()); uno::Sequence< rtl::OUString > aPropSourceNames(rNameMap.size());
uno::Sequence< rtl::OUString > aPropTargetNames(rNameMap.size()); uno::Sequence< rtl::OUString > aPropTargetNames(rNameMap.size());
......
...@@ -34,6 +34,12 @@ int main() ...@@ -34,6 +34,12 @@ int main()
int v1 = (static_cast<short>(1)) + 1; // expected-error {{unnecessary parentheses around cast [loplugin:unnecessaryparen]}} int v1 = (static_cast<short>(1)) + 1; // expected-error {{unnecessary parentheses around cast [loplugin:unnecessaryparen]}}
(void)v1; (void)v1;
// No warnings, used to silence -Wunreachable-code:
if ((false)) {
return 0;
}
x = (true) ? 0 : 1;
}; };
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
...@@ -22,7 +22,7 @@ struct Widget : public VclReferenceBase ...@@ -22,7 +22,7 @@ struct Widget : public VclReferenceBase
Widget* p = mpParent; Widget* p = mpParent;
(void)p; (void)p;
// test against false+ // test against false+
p = true ? mpParent.get() : nullptr; p = (true) ? mpParent.get() : nullptr;
} }
~Widget() override ~Widget() override
......
...@@ -224,6 +224,12 @@ void UnnecessaryParen::VisitSomeStmt(const Stmt *parent, const Expr* cond, Strin ...@@ -224,6 +224,12 @@ void UnnecessaryParen::VisitSomeStmt(const Stmt *parent, const Expr* cond, Strin
if (parenExpr) { if (parenExpr) {
if (parenExpr->getLocStart().isMacroID()) if (parenExpr->getLocStart().isMacroID())
return; return;
// Used to silence -Wunreachable-code:
if (isa<CXXBoolLiteralExpr>(parenExpr->getSubExpr())
&& stmtName == "if")
{
return;
}
// assignments need extra parentheses or they generate a compiler warning // assignments need extra parentheses or they generate a compiler warning
auto binaryOp = dyn_cast<BinaryOperator>(parenExpr->getSubExpr()); auto binaryOp = dyn_cast<BinaryOperator>(parenExpr->getSubExpr());
if (binaryOp && binaryOp->getOpcode() == BO_Assign) if (binaryOp && binaryOp->getOpcode() == BO_Assign)
......
...@@ -94,7 +94,7 @@ public: ...@@ -94,7 +94,7 @@ public:
void Test::testUnoType() { void Test::testUnoType() {
// Avoid warnings about unused ~DerivedInterface1/2 (see above): // Avoid warnings about unused ~DerivedInterface1/2 (see above):
if (false) { if ((false)) {
DerivedInterface1::dummy(nullptr); DerivedInterface1::dummy(nullptr);
DerivedInterface2::dummy(nullptr); DerivedInterface2::dummy(nullptr);
} }
......
...@@ -316,7 +316,9 @@ OUString AboutDialog::GetVersionString() ...@@ -316,7 +316,9 @@ OUString AboutDialog::GetVersionString()
sVersion += "\n" + Application::GetHWOSConfInfo(); sVersion += "\n" + Application::GetHWOSConfInfo();
if (EXTRA_BUILDID[0] != '\0') bool const extra = EXTRA_BUILDID[0] != '\0';
// extracted from the 'if' to avoid Clang -Wunreachable-code
if (extra)
{ {
sVersion += "\n" EXTRA_BUILDID; sVersion += "\n" EXTRA_BUILDID;
} }
......
...@@ -99,7 +99,7 @@ ScRange ScExponentialSmoothingDialog::ApplyOutput(ScDocShell* pDocShell) ...@@ -99,7 +99,7 @@ ScRange ScExponentialSmoothingDialog::ApplyOutput(ScDocShell* pDocShell)
output.nextRow(); output.nextRow();
// Initial value // Initial value
if (false) if ((false))
{ {
aTemplate.setTemplate("=AVERAGE(%RANGE%)"); aTemplate.setTemplate("=AVERAGE(%RANGE%)");
aTemplate.applyRange("%RANGE%", aCurrentRange); aTemplate.applyRange("%RANGE%", aCurrentRange);
......
...@@ -1207,6 +1207,9 @@ void SAL_CALL BluetoothServer::run() ...@@ -1207,6 +1207,9 @@ void SAL_CALL BluetoothServer::run()
while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status( pConnection )) while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status( pConnection ))
dbus_connection_dispatch( pConnection ); dbus_connection_dispatch( pConnection );
} }
if ((false)) break;
// silence Clang -Wunreachable-code after loop (TODO: proper
// fix?)
} }
unregisterBluez5Profile( pConnection ); unregisterBluez5Profile( pConnection );
g_main_context_unref( mpImpl->mpContext ); g_main_context_unref( mpImpl->mpContext );
...@@ -1295,6 +1298,8 @@ void SAL_CALL BluetoothServer::run() ...@@ -1295,6 +1298,8 @@ void SAL_CALL BluetoothServer::run()
pCommunicator->launch(); pCommunicator->launch();
} }
} }
if ((false)) break;
// silence Clang -Wunreachable-code after loop (TODO: proper fix?)
} }
unregisterBluez5Profile( pConnection ); unregisterBluez5Profile( pConnection );
......
...@@ -54,6 +54,7 @@ gb_CFLAGS_COMMON := \ ...@@ -54,6 +54,7 @@ gb_CFLAGS_COMMON := \
-Wextra \ -Wextra \
-Wstrict-prototypes \ -Wstrict-prototypes \
-Wundef \ -Wundef \
-Wunreachable-code \
-Wunused-macros \ -Wunused-macros \
-finput-charset=UTF-8 \ -finput-charset=UTF-8 \
-fmessage-length=0 \ -fmessage-length=0 \
...@@ -67,6 +68,7 @@ gb_CXXFLAGS_COMMON := \ ...@@ -67,6 +68,7 @@ gb_CXXFLAGS_COMMON := \
-Wendif-labels \ -Wendif-labels \
-Wextra \ -Wextra \
-Wundef \ -Wundef \
-Wunreachable-code \
-Wunused-macros \ -Wunused-macros \
-finput-charset=UTF-8 \ -finput-charset=UTF-8 \
-fmessage-length=0 \ -fmessage-length=0 \
......
...@@ -3985,7 +3985,7 @@ static bool lcl_SetOtherLineHeight( SwTableLine* pLine, CR_SetLineHeight& rParam ...@@ -3985,7 +3985,7 @@ static bool lcl_SetOtherLineHeight( SwTableLine* pLine, CR_SetLineHeight& rParam
// Calculate the new relative size by means of the old one // Calculate the new relative size by means of the old one
// If the selected Box get bigger, adjust via the max space else // If the selected Box get bigger, adjust via the max space else
// via the max height. // via the max height.
if( true /*!rParam.bBigger*/ ) if( (true) /*!rParam.bBigger*/ )
{ {
nDist *= pLineFrame->Frame().Height(); nDist *= pLineFrame->Frame().Height();
nDist /= rParam.nMaxHeight; nDist /= rParam.nMaxHeight;
......
...@@ -345,7 +345,7 @@ OUString SwUndoInsLayFormat::GetComment() const ...@@ -345,7 +345,7 @@ OUString SwUndoInsLayFormat::GetComment() const
// have a SwDrawContact yet, so it will fall back to SwUndo::GetComment(), // have a SwDrawContact yet, so it will fall back to SwUndo::GetComment(),
// which sets pComment to a wrong value. // which sets pComment to a wrong value.
// if (! pComment) // if (! pComment)
if (true) if ((true))
{ {
/* /*
If frame format is present and has an SdrObject use the undo If frame format is present and has an SdrObject use the undo
......
...@@ -1376,7 +1376,7 @@ OUString LocaleDataWrapper::getDate( const Date& rDate ) const ...@@ -1376,7 +1376,7 @@ OUString LocaleDataWrapper::getDate( const Date& rDate ) const
sal_Int16 nYear = rDate.GetYear(); sal_Int16 nYear = rDate.GetYear();
sal_uInt16 nYearLen; sal_uInt16 nYearLen;
if ( true /* IsDateCentury() */ ) if ( (true) /* IsDateCentury() */ )
nYearLen = 4; nYearLen = 4;
else else
{ {
...@@ -1490,7 +1490,7 @@ OUString LocaleDataWrapper::getDuration( const tools::Time& rTime, bool bSec, bo ...@@ -1490,7 +1490,7 @@ OUString LocaleDataWrapper::getDuration( const tools::Time& rTime, bool bSec, bo
if ( rTime < tools::Time( 0 ) ) if ( rTime < tools::Time( 0 ) )
pBuf = ImplAddString( pBuf, ' ' ); pBuf = ImplAddString( pBuf, ' ' );
if ( true /* IsTimeLeadingZero() */ ) if ( (true) /* IsTimeLeadingZero() */ )
pBuf = ImplAddUNum( pBuf, rTime.GetHour(), 2 ); pBuf = ImplAddUNum( pBuf, rTime.GetHour(), 2 );
else else
pBuf = ImplAddUNum( pBuf, rTime.GetHour() ); pBuf = ImplAddUNum( pBuf, rTime.GetHour() );
......
...@@ -102,7 +102,7 @@ void SAL_CALL CLiteral::initialize(const css::uno::Sequence< css::uno::Any > & a ...@@ -102,7 +102,7 @@ void SAL_CALL CLiteral::initialize(const css::uno::Sequence< css::uno::Any > & a
"CLiteral::initialize: argument must be string", *this, 0); "CLiteral::initialize: argument must be string", *this, 0);
} }
//FIXME: what is legal? //FIXME: what is legal?
if (true) { if ((true)) {
m_Value = arg0; m_Value = arg0;
} else { } else {
throw css::lang::IllegalArgumentException( throw css::lang::IllegalArgumentException(
......
...@@ -765,7 +765,7 @@ void SAL_CALL CURI::initialize(const css::uno::Sequence< css::uno::Any > & aArgu ...@@ -765,7 +765,7 @@ void SAL_CALL CURI::initialize(const css::uno::Sequence< css::uno::Any > & aArgu
"CURI::initialize: argument is not valid namespace", *this, 0); "CURI::initialize: argument is not valid namespace", *this, 0);
} }
//FIXME: what is legal? //FIXME: what is legal?
if (true) { if ((true)) {
m_LocalName = arg1; m_LocalName = arg1;
} else { } else {
throw css::lang::IllegalArgumentException( throw css::lang::IllegalArgumentException(
......
...@@ -99,7 +99,9 @@ extern "C" ...@@ -99,7 +99,9 @@ extern "C"
GtkYieldMutex *pYieldMutex; GtkYieldMutex *pYieldMutex;
// init gdk thread protection // init gdk thread protection
if ( !g_thread_supported() ) bool const sup = g_thread_supported();
// extracted from the 'if' to avoid Clang -Wunreachable-code
if ( !sup )
g_thread_init( nullptr ); g_thread_init( nullptr );
gdk_threads_set_lock_functions (GdkThreadsEnter, GdkThreadsLeave); gdk_threads_set_lock_functions (GdkThreadsEnter, GdkThreadsLeave);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment