From d5fbd552a6c2d424efe4766475678ebbd546733a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 1 May 2023 14:41:14 +0200 Subject: [PATCH] Refactoring: Break out loadLibraries() function. (#5024) No functional change is intended. This is a preparation to make qml handling work again --- cli/cppcheckexecutor.cpp | 65 +++++++++++++++++++++++----------------- cli/cppcheckexecutor.h | 7 +++++ 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 15706ce50..fd17b53c7 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -255,36 +255,9 @@ bool CppCheckExecutor::reportSuppressions(const Settings &settings, bool unusedF int CppCheckExecutor::check_internal(CppCheck& cppcheck) { Settings& settings = cppcheck.settings(); - const bool std = tryLoadLibrary(settings.library, settings.exename, "std.cfg"); - auto failed_lib = std::find_if(settings.libraries.begin(), settings.libraries.end(), [&](const std::string& lib) { - return !tryLoadLibrary(settings.library, settings.exename, lib.c_str()); - }); - if (failed_lib != settings.libraries.end()) { - const std::string msg("Failed to load the library " + *failed_lib); - const std::list callstack; - ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", Certainty::normal); - reportErr(errmsg); + if (!loadLibraries(settings)) return EXIT_FAILURE; - } - - if (!std) { - const std::list callstack; - const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install."); -#ifdef FILESDIR - const std::string details("The Cppcheck binary was compiled with FILESDIR set to \"" - FILESDIR "\" and will therefore search for " - "std.cfg in " FILESDIR "/cfg."); -#else - const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(settings.exename)) + "cfg"); - const std::string details("The Cppcheck binary was compiled without FILESDIR set. Either the " - "std.cfg should be available in " + cfgfolder + " or the FILESDIR " - "should be configured."); -#endif - ErrorMessage errmsg(callstack, emptyString, Severity::information, msg+" "+details, "failedToLoadCfg", Certainty::normal); - reportErr(errmsg); - return EXIT_FAILURE; - } if (settings.reportProgress) mLatestProgressOutputTime = std::time(nullptr); @@ -341,6 +314,42 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck) return 0; } +bool CppCheckExecutor::loadLibraries(Settings& settings) +{ + const bool std = tryLoadLibrary(settings.library, settings.exename, "std.cfg"); + + const auto failed_lib = std::find_if(settings.libraries.begin(), settings.libraries.end(), [&](const std::string& lib) { + return !tryLoadLibrary(settings.library, settings.exename, lib.c_str()); + }); + if (failed_lib != settings.libraries.end()) { + const std::string msg("Failed to load the library " + *failed_lib); + const std::list callstack; + ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", Certainty::normal); + reportErr(errmsg); + return false; + } + + if (!std) { + const std::list callstack; + const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install."); +#ifdef FILESDIR + const std::string details("The Cppcheck binary was compiled with FILESDIR set to \"" + FILESDIR "\" and will therefore search for " + "std.cfg in " FILESDIR "/cfg."); +#else + const std::string cfgfolder(Path::fromNativeSeparators(Path::getPathFromFilename(settings.exename)) + "cfg"); + const std::string details("The Cppcheck binary was compiled without FILESDIR set. Either the " + "std.cfg should be available in " + cfgfolder + " or the FILESDIR " + "should be configured."); +#endif + ErrorMessage errmsg(callstack, emptyString, Severity::information, msg+" "+details, "failedToLoadCfg", Certainty::normal); + reportErr(errmsg); + return false; + } + + return true; +} + #ifdef _WIN32 // fix trac ticket #439 'Cppcheck reports wrong filename for filenames containing 8-bit ASCII' static inline std::string ansiToOEM(const std::string &msg, bool doConvert) diff --git a/cli/cppcheckexecutor.h b/cli/cppcheckexecutor.h index ae7d8f54c..1e94aa58d 100644 --- a/cli/cppcheckexecutor.h +++ b/cli/cppcheckexecutor.h @@ -144,6 +144,13 @@ private: */ int check_internal(CppCheck& cppcheck); + /** + * @brief Load libraries + * @param settings Settings + * @return Returns true if successful + */ + bool loadLibraries(Settings& settings); + /** * Pointer to current settings; set while check() is running for reportError(). */