Fix #10866 (no preprocessorErrorDirective and code generated in case of unconditional #error) (#5670)

This commit is contained in:
Daniel Marjamäki 2023-11-16 17:25:49 +01:00 committed by GitHub
parent e47300016b
commit e01e090f1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -930,6 +930,24 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
// #error etc during preprocessing // #error etc during preprocessing
configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg); configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
--checkCount; // don't count invalid configurations --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<ErrorMessage::FileLocation> callstack(1, loc1);
ErrorMessage errmsg(callstack,
filename,
Severity::error,
o.msg,
"preprocessorErrorDirective",
Certainty::normal);
reportErr(errmsg);
}
continue; continue;
} catch (const TerminateException &) { } catch (const TerminateException &) {

View File

@ -71,6 +71,15 @@ def test_missing_include_inline_suppr(tmpdir):
assert stderr == '' 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): def test_invalid_library(tmpdir):
args = ['--library=none', '--library=posix', '--library=none2', 'file.c'] args = ['--library=none', '--library=posix', '--library=none2', 'file.c']