From 86d298f87f00fe70d31a65b7e98f9b17fa80cb6b Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Fri, 2 Apr 2021 15:12:00 +0300 Subject: [PATCH] Simplify some static variable initializations Change-Id: I6cdd44aa66b9597ccc51fc3fd69b57485ccb3230 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113515 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- basctl/source/dlged/dlgedfac.cxx | 13 +++++------ basic/source/classes/sbunoobj.cxx | 14 +++++------- basic/source/runtime/iosys.cxx | 14 +++++------- basic/source/runtime/methods1.cxx | 17 +++++--------- comphelper/source/misc/lok.cxx | 14 +++++------- connectivity/source/drivers/file/fcomp.cxx | 4 +--- desktop/source/app/crashreport.cxx | 10 +-------- sc/source/ui/unoobj/textuno.cxx | 26 ++++++++++------------ sfx2/source/appl/appinit.cxx | 22 +++++++++--------- 9 files changed, 50 insertions(+), 84 deletions(-) diff --git a/basctl/source/dlged/dlgedfac.cxx b/basctl/source/dlged/dlgedfac.cxx index 38c8dc0d1a9c..98c0f260b0f9 100644 --- a/basctl/source/dlged/dlgedfac.cxx +++ b/basctl/source/dlged/dlgedfac.cxx @@ -46,20 +46,17 @@ DlgEdFactory::~DlgEdFactory() COVERITY_NOEXCEPT_FALSE IMPL_LINK( DlgEdFactory, MakeObject, SdrObjCreatorParams, aParams, SdrObject* ) { - static bool bNeedsInit = true; - static uno::Reference< lang::XMultiServiceFactory > xDialogSFact; - - if( bNeedsInit ) - { + static const uno::Reference xDialogSFact = [] { + uno::Reference xFact; uno::Reference< uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext(); uno::Reference< container::XNameContainer > xC( xContext->getServiceManager()->createInstanceWithContext( "com.sun.star.awt.UnoControlDialogModel", xContext ), uno::UNO_QUERY ); if( xC.is() ) { uno::Reference< lang::XMultiServiceFactory > xModFact( xC, uno::UNO_QUERY ); - xDialogSFact = xModFact; + xFact = xModFact; } - bNeedsInit = false; - } + return xFact; + }(); SdrObject* pNewObj = nullptr; if( (aParams.nInventor == SdrInventor::BasicDialog) && diff --git a/basic/source/classes/sbunoobj.cxx b/basic/source/classes/sbunoobj.cxx index 9f00a8f231c9..fcd491462528 100644 --- a/basic/source/classes/sbunoobj.cxx +++ b/basic/source/classes/sbunoobj.cxx @@ -233,22 +233,18 @@ static Reference< XTypeConverter > const & getTypeConverter_Impl() // #111851 factory function to create an OLE object SbUnoObject* createOLEObject_Impl( const OUString& aType ) { - static Reference< XMultiServiceFactory > xOLEFactory; - static bool bNeedsInit = true; - - if( bNeedsInit ) - { - bNeedsInit = false; - + static const Reference xOLEFactory = [] { + Reference xFactory; Reference< XComponentContext > xContext( comphelper::getProcessComponentContext() ); if( xContext.is() ) { Reference xSMgr = xContext->getServiceManager(); - xOLEFactory.set( + xFactory.set( xSMgr->createInstanceWithContext( "com.sun.star.bridge.OleObjectFactory", xContext ), UNO_QUERY ); } - } + return xFactory; + }(); SbUnoObject* pUnoObj = nullptr; if( xOLEFactory.is() ) diff --git a/basic/source/runtime/iosys.cxx b/basic/source/runtime/iosys.cxx index 4c991c6676c7..0cf83c509a39 100644 --- a/basic/source/runtime/iosys.cxx +++ b/basic/source/runtime/iosys.cxx @@ -133,17 +133,12 @@ void SbiStream::MapError() // #89378 New semantic: Don't just ask for UNO but for UCB bool hasUno() { - static bool bNeedInit = true; - static bool bRetVal = true; - - if( bNeedInit ) - { - bNeedInit = false; + static const bool bRetVal = [] { Reference< XComponentContext > xContext = comphelper::getProcessComponentContext(); if( !xContext.is() ) { // No service manager at all - bRetVal = false; + return false; } else { @@ -152,10 +147,11 @@ bool hasUno() if ( !( xManager->queryContentProvider( "file:///" ).is() ) ) { // No UCB - bRetVal = false; + return false; } } - } + return true; + }(); return bRetVal; } diff --git a/basic/source/runtime/methods1.cxx b/basic/source/runtime/methods1.cxx index 2f89a90a8149..9addf7140adf 100644 --- a/basic/source/runtime/methods1.cxx +++ b/basic/source/runtime/methods1.cxx @@ -71,23 +71,16 @@ static Reference< XCalendar4 > const & getLocaleCalendar() { static Reference< XCalendar4 > xCalendar = LocaleCalendar2::create(getProcessComponentContext()); static css::lang::Locale aLastLocale; - static bool bNeedsInit = true; + static bool bNeedsReload = true; css::lang::Locale aLocale = Application::GetSettings().GetLanguageTag().getLocale(); - bool bNeedsReload = false; - if( bNeedsInit ) - { - bNeedsInit = false; - bNeedsReload = true; - } - else if( aLocale.Language != aLastLocale.Language || + bNeedsReload = bNeedsReload || + ( aLocale.Language != aLastLocale.Language || aLocale.Country != aLastLocale.Country || - aLocale.Variant != aLastLocale.Variant ) - { - bNeedsReload = true; - } + aLocale.Variant != aLastLocale.Variant ); if( bNeedsReload ) { + bNeedsReload = false; aLastLocale = aLocale; xCalendar->loadDefaultCalendar( aLocale ); } diff --git a/comphelper/source/misc/lok.cxx b/comphelper/source/misc/lok.cxx index 796a3dc3b839..fe7c742e4ec3 100644 --- a/comphelper/source/misc/lok.cxx +++ b/comphelper/source/misc/lok.cxx @@ -214,10 +214,8 @@ bool isAllowlistedLanguage(const OUString& lang) (void) lang; return true; #else - static bool bInitialized = false; - static std::vector aAllowlist; - if (!bInitialized) - { + static const std::vector aAllowlist = [] { + std::vector aList; // coverity[tainted_data] - we trust the contents of this variable const char* pAllowlist = getenv("LOK_ALLOWLIST_LANGUAGES"); if (pAllowlist) @@ -231,16 +229,16 @@ bool isAllowlistedLanguage(const OUString& lang) continue; std::cerr << s << " "; - aAllowlist.emplace_back(OStringToOUString(s.c_str(), RTL_TEXTENCODING_UTF8)); + aList.emplace_back(OStringToOUString(s.c_str(), RTL_TEXTENCODING_UTF8)); } std::cerr << std::endl; } - if (aAllowlist.empty()) + if (aList.empty()) std::cerr << "No language allowlisted, turning off the language support." << std::endl; - bInitialized = true; - } + return aList; + }(); if (aAllowlist.empty()) return false; diff --git a/connectivity/source/drivers/file/fcomp.cxx b/connectivity/source/drivers/file/fcomp.cxx index 982d0266b949..44d175be69f3 100644 --- a/connectivity/source/drivers/file/fcomp.cxx +++ b/connectivity/source/drivers/file/fcomp.cxx @@ -533,8 +533,6 @@ OOperand* OPredicateCompiler::execute_Operand(OSQLParseNode const * pPredicateNo bool OPredicateInterpreter::evaluate(OCodeList& rCodeList) { - static bool bResult; - if (!(rCodeList[0])) return true; // no Predicate @@ -553,7 +551,7 @@ bool OPredicateInterpreter::evaluate(OCodeList& rCodeList) DBG_ASSERT(m_aStack.empty(), "Stack error"); DBG_ASSERT(pOperand, "Stack error"); - bResult = pOperand->isValid(); + const bool bResult = pOperand->isValid(); if (typeid(OOperandResult) == typeid(*pOperand)) delete pOperand; return bResult; diff --git a/desktop/source/app/crashreport.cxx b/desktop/source/app/crashreport.cxx index ff9c80c51996..84ceabb619bd 100644 --- a/desktop/source/app/crashreport.cxx +++ b/desktop/source/app/crashreport.cxx @@ -200,15 +200,7 @@ void CrashReporter::updateMinidumpLocation() bool CrashReporter::crashReportInfoExists() { - static bool first = true; - static bool InfoExist = false; - - if (first) - { - first = false; - InfoExist = crashreport::readConfig(CrashReporter::getIniFileName(), nullptr); - } - + static const bool InfoExist = crashreport::readConfig(CrashReporter::getIniFileName(), nullptr); return InfoExist; } diff --git a/sc/source/ui/unoobj/textuno.cxx b/sc/source/ui/unoobj/textuno.cxx index 8af20f45485b..18f40a764ce1 100644 --- a/sc/source/ui/unoobj/textuno.cxx +++ b/sc/source/ui/unoobj/textuno.cxx @@ -51,18 +51,16 @@ using namespace com::sun::star; static const SvxItemPropertySet * lcl_GetHdFtPropertySet() { - static SfxItemPropertyMapEntry aHdFtPropertyMap_Impl[] = - { - SVX_UNOEDIT_CHAR_PROPERTIES, - SVX_UNOEDIT_FONT_PROPERTIES, - SVX_UNOEDIT_PARA_PROPERTIES, - SVX_UNOEDIT_NUMBERING_PROPERTIE, // for completeness of service ParagraphProperties - { u"", 0, css::uno::Type(), 0, 0 } - }; - static bool bTwipsSet = false; + static const SvxItemPropertySet aHdFtPropertySet_Impl = [] { + SfxItemPropertyMapEntry aHdFtPropertyMap_Impl[] = + { + SVX_UNOEDIT_CHAR_PROPERTIES, + SVX_UNOEDIT_FONT_PROPERTIES, + SVX_UNOEDIT_PARA_PROPERTIES, + SVX_UNOEDIT_NUMBERING_PROPERTIE, // for completeness of service ParagraphProperties + { u"", 0, css::uno::Type(), 0, 0 } + }; - if (!bTwipsSet) - { // modify PropertyMap to include CONVERT_TWIPS flag for font height // (headers/footers are in twips) @@ -79,9 +77,9 @@ static const SvxItemPropertySet * lcl_GetHdFtPropertySet() ++pEntry; } - bTwipsSet = true; - } - static SvxItemPropertySet aHdFtPropertySet_Impl( aHdFtPropertyMap_Impl, SdrObject::GetGlobalDrawObjectItemPool() ); + + return SvxItemPropertySet(aHdFtPropertyMap_Impl, SdrObject::GetGlobalDrawObjectItemPool()); + }(); return &aHdFtPropertySet_Impl; } diff --git a/sfx2/source/appl/appinit.cxx b/sfx2/source/appl/appinit.cxx index fbecd9d66b61..97b85ef84a92 100644 --- a/sfx2/source/appl/appinit.cxx +++ b/sfx2/source/appl/appinit.cxx @@ -147,30 +147,28 @@ extern "C" bool GetSpecialCharsForEdit(weld::Widget* i_pParent, const vcl::Font& static OUString SfxGetSpecialCharsForEdit(weld::Widget* pParent, const vcl::Font& rFont) { - static bool bDetermineFunction = false; - static PFunc_getSpecialCharsForEdit pfunc_getSpecialCharsForEdit = nullptr; - - SolarMutexGuard aGuard; - if ( !bDetermineFunction ) - { - bDetermineFunction = true; - + static const PFunc_getSpecialCharsForEdit pfunc_getSpecialCharsForEdit = [] { + PFunc_getSpecialCharsForEdit pfunc = nullptr; #ifndef DISABLE_DYNLOADING osl::Module aMod; aMod.loadRelative(&thisModule, SVLIBRARY("cui")); // get symbol - pfunc_getSpecialCharsForEdit = reinterpret_cast(aMod.getFunctionSymbol("GetSpecialCharsForEdit")); - DBG_ASSERT( pfunc_getSpecialCharsForEdit, "GetSpecialCharsForEdit() not found!" ); + pfunc = reinterpret_cast(aMod.getFunctionSymbol("GetSpecialCharsForEdit")); + DBG_ASSERT( pfunc, "GetSpecialCharsForEdit() not found!" ); aMod.release(); #else - pfunc_getSpecialCharsForEdit = GetSpecialCharsForEdit; + pfunc = GetSpecialCharsForEdit; #endif - } + return pfunc; + }(); OUString aRet; if ( pfunc_getSpecialCharsForEdit ) + { + SolarMutexGuard aGuard; (*pfunc_getSpecialCharsForEdit)( pParent, rFont, aRet ); + } return aRet; }