diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index d69e0ea7f..7cc42b6a5 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -930,6 +930,24 @@ 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.. + 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 95aaaa654..fcdf75392 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -71,6 +71,15 @@ 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') + exitcode, _, stderr = cppcheck(['--error-exitcode=1', test_file]) + assert 'preprocessorErrorDirective' in stderr + assert exitcode != 0 + + def test_invalid_library(tmpdir): args = ['--library=none', '--library=posix', '--library=none2', 'file.c']