Skip to content

Commit cc592a6

Browse files
authored
CppCheckExecutor: improved library loading error handling a bit (#5275)
1 parent e38a031 commit cc592a6

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#endif
4444

4545
#include <algorithm>
46+
#include <cassert>
4647
#include <cstdio>
4748
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
4849
#include <functional>
@@ -66,6 +67,8 @@
6667
#include <windows.h>
6768
#endif
6869

70+
// TODO: do not directly write to stdout
71+
6972

7073
/*static*/ FILE* CppCheckExecutor::mExceptionOutput = stdout;
7174

@@ -319,21 +322,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
319322

320323
bool CppCheckExecutor::loadLibraries(Settings& settings)
321324
{
322-
const bool std = tryLoadLibrary(settings.library, settings.exename, "std.cfg");
323-
324-
const auto failed_lib = std::find_if(settings.libraries.begin(), settings.libraries.end(), [&](const std::string& lib) {
325-
return !tryLoadLibrary(settings.library, settings.exename, lib.c_str());
326-
});
327-
if (failed_lib != settings.libraries.end()) {
328-
const std::string msg("Failed to load the library " + *failed_lib);
329-
const std::list<ErrorMessage::FileLocation> callstack;
330-
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", Certainty::normal);
331-
reportErr(errmsg);
332-
return false;
333-
}
334-
335-
if (!std) {
336-
const std::list<ErrorMessage::FileLocation> callstack;
325+
if (!tryLoadLibrary(settings.library, settings.exename, "std.cfg")) {
337326
const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install.");
338327
#ifdef FILESDIR
339328
const std::string details("The Cppcheck binary was compiled with FILESDIR set to \""
@@ -345,12 +334,17 @@ bool CppCheckExecutor::loadLibraries(Settings& settings)
345334
"std.cfg should be available in " + cfgfolder + " or the FILESDIR "
346335
"should be configured.");
347336
#endif
348-
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg+" "+details, "failedToLoadCfg", Certainty::normal);
349-
reportErr(errmsg);
337+
std::cout << msg << " " << details << std::endl;
350338
return false;
351339
}
352340

353-
return true;
341+
bool result = true;
342+
for (const auto& lib : settings.libraries) {
343+
if (!tryLoadLibrary(settings.library, settings.exename, lib.c_str())) {
344+
result = false;
345+
}
346+
}
347+
return result;
354348
}
355349

356350
#ifdef _WIN32
@@ -424,6 +418,8 @@ void CppCheckExecutor::reportErr(const ErrorMessage &msg)
424418
return;
425419
}
426420

421+
assert(mSettings != nullptr);
422+
427423
// Alert only about unique errors
428424
if (!mShownErrors.insert(msg.toString(mSettings->verbose)).second)
429425
return;

test/cli/test-other.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,15 @@ def test_missing_include_inline_suppr(tmpdir):
6060
args = ['--enable=missingInclude', '--inline-suppr', test_file]
6161

6262
_, _, stderr = cppcheck(args)
63-
assert stderr == ''
63+
assert stderr == ''
64+
65+
def test_invalid_library(tmpdir):
66+
args = ['--library=none', '--library=posix', '--library=none2', '--platform=native', 'file.c']
67+
68+
exitcode, stdout, stderr = cppcheck(args)
69+
assert exitcode == 1
70+
assert (stdout == "cppcheck: Failed to load library configuration file 'none'. File not found\n"
71+
"cppcheck: Failed to load library configuration file 'none2'. File not found\n")
72+
assert stderr == ""
73+
74+
# TODO: test missing std.cfg

0 commit comments

Comments
 (0)