Skip to content

Commit f1bb5ad

Browse files
committed
fixed safety override
1 parent 6b066ea commit f1bb5ad

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

cli/cmdlineparser.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10221022
mSettings.cppHeaderProbe = false;
10231023
}
10241024

1025+
else if (std::strcmp(argv[i], "--no-safety") == 0)
1026+
mSettings.safety = false;
1027+
10251028
// Write results in file
10261029
else if (std::strncmp(argv[i], "--output-file=", 14) == 0)
10271030
mSettings.outputFile = Path::simplifyPath(argv[i] + 14);

lib/settings.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppress
162162
const auto& v = it->second;
163163
if (!v.is<bool>())
164164
return "'safety' is not a bool";
165-
settings.safety = settings.safety || v.get<bool>();
165+
settings.safety = v.get<bool>();
166166
}
167167
}
168168

test/cli/other_test.py

+21
Original file line numberDiff line numberDiff line change
@@ -2026,6 +2026,27 @@ def test_config_invalid(tmpdir):
20262026
]
20272027

20282028

2029+
def test_config_override(tmpdir):
2030+
# cppcheck.cfg needs to be next to executable
2031+
exe = shutil.copy2(__lookup_cppcheck_exe(), tmpdir)
2032+
shutil.copytree(os.path.join(os.path.dirname(__lookup_cppcheck_exe()), 'cfg'), os.path.join(tmpdir, 'cfg'))
2033+
2034+
test_file = os.path.join(tmpdir, 'test.c')
2035+
with open(test_file, 'wt'):
2036+
pass
2037+
2038+
config_file = os.path.join(tmpdir, 'cppcheck.cfg')
2039+
with open(config_file, 'wt') as f:
2040+
f.write(json.dumps({
2041+
'safety': False
2042+
}))
2043+
2044+
exitcode, stdout, stderr, exe = cppcheck_ex(['-q', '--safety', test_file], cwd=tmpdir, cppcheck_exe=exe, remove_checkers_report=False)
2045+
assert exitcode == 0, stdout if stdout else stderr
2046+
assert stdout.splitlines() == []
2047+
assert stderr.splitlines() == []
2048+
2049+
20292050
def test_checkers_report(tmpdir):
20302051
test_file = os.path.join(tmpdir, 'test.c')
20312052
with open(test_file, 'wt') as f:

test/testcmdlineparser.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ class TestCmdlineParser : public TestFixture {
440440
TEST_CASE(debugClangOutput);
441441
TEST_CASE(debugXmlMultiple);
442442
TEST_CASE(debugNormalXmlMultiple);
443+
TEST_CASE(safety);
444+
TEST_CASE(safetyOverride);
445+
TEST_CASE(noSafety);
446+
TEST_CASE(noSafetyOverride);
443447

444448
TEST_CASE(ignorepaths1);
445449
TEST_CASE(ignorepaths2);
@@ -2974,6 +2978,34 @@ class TestCmdlineParser : public TestFixture {
29742978
ASSERT_EQUALS("cppcheck: error: printing debug output in XML format does not support multiple input files.\n", logger->str());
29752979
}
29762980

2981+
void safety() {
2982+
REDIRECT;
2983+
const char * const argv[] = {"cppcheck", "--safety", "file.cpp"};
2984+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
2985+
ASSERT_EQUALS(true, settings->safety);
2986+
}
2987+
2988+
void safetyOverride() {
2989+
REDIRECT;
2990+
const char * const argv[] = {"cppcheck", "--no-safety", "--safety", "file.cpp"};
2991+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
2992+
ASSERT_EQUALS(true, settings->safety);
2993+
}
2994+
2995+
void noSafety() {
2996+
REDIRECT;
2997+
const char * const argv[] = {"cppcheck", "--no-safety", "file.cpp"};
2998+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
2999+
ASSERT_EQUALS(false, settings->safety);
3000+
}
3001+
3002+
void noSafetyOverride() {
3003+
REDIRECT;
3004+
const char * const argv[] = {"cppcheck", "--safety", "--no-safety", "file.cpp"};
3005+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3006+
ASSERT_EQUALS(false, settings->safety);
3007+
}
3008+
29773009
void ignorepaths1() {
29783010
REDIRECT;
29793011
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

0 commit comments

Comments
 (0)