Skip to content

Commit e01e090

Browse files
authored
Fix #10866 (no preprocessorErrorDirective and code generated in case of unconditional #error) (#5670)
1 parent e473000 commit e01e090

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/cppcheck.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,24 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
930930
// #error etc during preprocessing
931931
configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
932932
--checkCount; // don't count invalid configurations
933+
934+
if (!hasValidConfig && currCfg == *configurations.rbegin()) {
935+
// If there is no valid configuration then report error..
936+
std::string file = Path::fromNativeSeparators(o.location.file());
937+
if (mSettings.relativePaths)
938+
file = Path::getRelativePath(file, mSettings.basePaths);
939+
940+
const ErrorMessage::FileLocation loc1(file, o.location.line, o.location.col);
941+
std::list<ErrorMessage::FileLocation> callstack(1, loc1);
942+
943+
ErrorMessage errmsg(callstack,
944+
filename,
945+
Severity::error,
946+
o.msg,
947+
"preprocessorErrorDirective",
948+
Certainty::normal);
949+
reportErr(errmsg);
950+
}
933951
continue;
934952

935953
} catch (const TerminateException &) {

test/cli/test-other.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ def test_missing_include_inline_suppr(tmpdir):
7171
assert stderr == ''
7272

7373

74+
def test_preprocessor_error(tmpdir):
75+
test_file = os.path.join(tmpdir, '10866.c')
76+
with open(test_file, 'wt') as f:
77+
f.write('#error test\nx=1;\n')
78+
exitcode, _, stderr = cppcheck(['--error-exitcode=1', test_file])
79+
assert 'preprocessorErrorDirective' in stderr
80+
assert exitcode != 0
81+
82+
7483
def test_invalid_library(tmpdir):
7584
args = ['--library=none', '--library=posix', '--library=none2', 'file.c']
7685

0 commit comments

Comments
 (0)