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
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
#include <functional>
@ -66,6 +67,8 @@
#include <windows.h>
#endif
// TODO: do not directly write to stdout
/*static*/ FILE* CppCheckExecutor::mExceptionOutput = stdout;
@ -319,21 +322,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
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<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;
if (!tryLoadLibrary(settings.library, settings.exename, "std.cfg")) {
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 \""
@ -345,12 +334,17 @@ bool CppCheckExecutor::loadLibraries(Settings& settings)
"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);
std::cout << msg << " " << details << std::endl;
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
@ -424,6 +418,8 @@ void CppCheckExecutor::reportErr(const ErrorMessage &msg)
return;
}
assert(mSettings != nullptr);
// Alert only about unique errors
if (!mShownErrors.insert(msg.toString(mSettings->verbose)).second)
return;

View File

@ -60,4 +60,15 @@ def test_missing_include_inline_suppr(tmpdir):
args = ['--enable=missingInclude', '--inline-suppr', test_file]
_, _, 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