CppCheckExecutor: improved library loading error handling a bit (#5275)

This commit is contained in:
Oliver Stöneberg 2023-08-07 19:48:11 +02:00 committed by GitHub
parent e38a031ae6
commit cc592a6927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 19 deletions

View File

@ -43,6 +43,7 @@
#endif #endif
#include <algorithm> #include <algorithm>
#include <cassert>
#include <cstdio> #include <cstdio>
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE #include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
#include <functional> #include <functional>
@ -66,6 +67,8 @@
#include <windows.h> #include <windows.h>
#endif #endif
// TODO: do not directly write to stdout
/*static*/ FILE* CppCheckExecutor::mExceptionOutput = stdout; /*static*/ FILE* CppCheckExecutor::mExceptionOutput = stdout;
@ -319,21 +322,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
bool CppCheckExecutor::loadLibraries(Settings& settings) bool CppCheckExecutor::loadLibraries(Settings& settings)
{ {
const bool std = tryLoadLibrary(settings.library, settings.exename, "std.cfg"); if (!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<ErrorMessage::FileLocation> callstack;
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", Certainty::normal);
reportErr(errmsg);
return false;
}
if (!std) {
const std::list<ErrorMessage::FileLocation> callstack;
const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install."); const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install.");
#ifdef FILESDIR #ifdef FILESDIR
const std::string details("The Cppcheck binary was compiled with FILESDIR set to \"" const std::string details("The Cppcheck binary was compiled with FILESDIR set to \""
@ -345,12 +334,17 @@ bool CppCheckExecutor::loadLibraries(Settings& settings)
"std.cfg should be available in " + cfgfolder + " or the FILESDIR " "std.cfg should be available in " + cfgfolder + " or the FILESDIR "
"should be configured."); "should be configured.");
#endif #endif
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg+" "+details, "failedToLoadCfg", Certainty::normal); std::cout << msg << " " << details << std::endl;
reportErr(errmsg);
return false; return false;
} }
return true; bool result = true;
for (const auto& lib : settings.libraries) {
if (!tryLoadLibrary(settings.library, settings.exename, lib.c_str())) {
result = false;
}
}
return result;
} }
#ifdef _WIN32 #ifdef _WIN32
@ -424,6 +418,8 @@ void CppCheckExecutor::reportErr(const ErrorMessage &msg)
return; return;
} }
assert(mSettings != nullptr);
// Alert only about unique errors // Alert only about unique errors
if (!mShownErrors.insert(msg.toString(mSettings->verbose)).second) if (!mShownErrors.insert(msg.toString(mSettings->verbose)).second)
return; return;

View File

@ -61,3 +61,14 @@ def test_missing_include_inline_suppr(tmpdir):
_, _, stderr = cppcheck(args) _, _, stderr = cppcheck(args)
assert stderr == '' assert stderr == ''
def test_invalid_library(tmpdir):
args = ['--library=none', '--library=posix', '--library=none2', '--platform=native', 'file.c']
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 1
assert (stdout == "cppcheck: Failed to load library configuration file 'none'. File not found\n"
"cppcheck: Failed to load library configuration file 'none2'. File not found\n")
assert stderr == ""
# TODO: test missing std.cfg