From 538e77ef602966ea86b9c88650647ea9b2a755ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 16 Nov 2023 15:11:07 +0100 Subject: [PATCH 1/2] Fix #10866 (no preprocessorErrorDirective and code generated in case of unconditional #error) --- lib/cppcheck.cpp | 20 ++++++++++++++++++++ test/cli/test-other.py | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index d69e0ea7fc2..fcf01fc0ac2 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -930,6 +930,26 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string // #error etc during preprocessing configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg); --checkCount; // don't count invalid configurations + + if (!hasValidConfig && currCfg == *configurations.rbegin()) { + // If there is no valid configuration then report error.. + mExitCode = 1; + + std::string file = Path::fromNativeSeparators(o.location.file()); + if (mSettings.relativePaths) + file = Path::getRelativePath(file, mSettings.basePaths); + + const ErrorMessage::FileLocation loc1(file, o.location.line, o.location.col); + std::list callstack(1, loc1); + + ErrorMessage errmsg(callstack, + filename, + Severity::error, + o.msg, + "preprocessorErrorDirective", + Certainty::normal); + reportErr(errmsg); + } continue; } catch (const TerminateException &) { diff --git a/test/cli/test-other.py b/test/cli/test-other.py index 95aaaa6545c..f89c63b630f 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -71,6 +71,14 @@ def test_missing_include_inline_suppr(tmpdir): assert stderr == '' +def test_preprocessor_error(tmpdir): + test_file = os.path.join(tmpdir, '10866.c') + with open(test_file, 'wt') as f: + f.write('#error test\nx=1;\n') + _, _, stderr = cppcheck([test_file]) + assert 'preprocessorErrorDirective' in stderr + + def test_invalid_library(tmpdir): args = ['--library=none', '--library=posix', '--library=none2', 'file.c'] From 3cdd03ea18317f21faed0a479c611d733e683a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 16 Nov 2023 16:18:42 +0100 Subject: [PATCH 2/2] fix review comments --- lib/cppcheck.cpp | 2 -- test/cli/test-other.py | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index fcf01fc0ac2..7cc42b6a54c 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -933,8 +933,6 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string if (!hasValidConfig && currCfg == *configurations.rbegin()) { // If there is no valid configuration then report error.. - mExitCode = 1; - std::string file = Path::fromNativeSeparators(o.location.file()); if (mSettings.relativePaths) file = Path::getRelativePath(file, mSettings.basePaths); diff --git a/test/cli/test-other.py b/test/cli/test-other.py index f89c63b630f..fcdf7539295 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -75,8 +75,9 @@ def test_preprocessor_error(tmpdir): test_file = os.path.join(tmpdir, '10866.c') with open(test_file, 'wt') as f: f.write('#error test\nx=1;\n') - _, _, stderr = cppcheck([test_file]) + exitcode, _, stderr = cppcheck(['--error-exitcode=1', test_file]) assert 'preprocessorErrorDirective' in stderr + assert exitcode != 0 def test_invalid_library(tmpdir):