Skip to content

Commit 536bd64

Browse files
committed
allow safety from config to be overwritten
1 parent da20e5e commit 536bd64

5 files changed

Lines changed: 59 additions & 3 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10161016
mSettings.cppHeaderProbe = false;
10171017
}
10181018

1019+
else if (std::strcmp(argv[i], "--no-safety") == 0)
1020+
mSettings.safety = false;
1021+
10191022
// Write results in file
10201023
else if (std::strncmp(argv[i], "--output-file=", 14) == 0)
10211024
mSettings.outputFile = Path::simplifyPath(argv[i] + 14);
@@ -1116,7 +1119,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11161119
"misra-c++-2023",
11171120
"misra-cpp-2023",
11181121
"bughunting",
1119-
"safety",
1122+
"safety", // TODO: deprecate in favor of the regular --saftey/--no-safety
11201123
"debug-progress"};
11211124
// valid options --premium-..=
11221125
const std::set<std::string> valid2{

lib/settings.cpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,27 @@ def test_config_invalid(tmpdir):
17431743
]
17441744

17451745

1746+
def test_config_override(tmpdir):
1747+
# cppcheck.cfg needs to be next to executable
1748+
exe = shutil.copy2(__lookup_cppcheck_exe(), tmpdir)
1749+
shutil.copytree(os.path.join(os.path.dirname(__lookup_cppcheck_exe()), 'cfg'), os.path.join(tmpdir, 'cfg'))
1750+
1751+
test_file = os.path.join(tmpdir, 'test.c')
1752+
with open(test_file, 'wt'):
1753+
pass
1754+
1755+
config_file = os.path.join(tmpdir, 'cppcheck.cfg')
1756+
with open(config_file, 'wt') as f:
1757+
f.write(json.dumps({
1758+
'safety': False
1759+
}))
1760+
1761+
exitcode, stdout, stderr, exe = cppcheck_ex(['-q', '--safety', test_file], cwd=tmpdir, cppcheck_exe=exe, remove_checkers_report=False)
1762+
assert exitcode == 0, stdout if stdout else stderr
1763+
assert stdout.splitlines() == []
1764+
assert 'checkersReport' in stderr
1765+
1766+
17461767
def test_checkers_report(tmpdir):
17471768
test_file = os.path.join(tmpdir, 'test.c')
17481769
with open(test_file, 'wt') as f:

test/testcmdlineparser.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ class TestCmdlineParser : public TestFixture {
489489
TEST_CASE(debugNormalVerbose);
490490
TEST_CASE(debug);
491491
TEST_CASE(debugVerbose);
492+
TEST_CASE(safety);
493+
TEST_CASE(safetyOverride);
494+
TEST_CASE(noSafety);
495+
TEST_CASE(noSafetyOverride);
492496

493497
TEST_CASE(ignorepaths1);
494498
TEST_CASE(ignorepaths2);
@@ -3389,6 +3393,34 @@ class TestCmdlineParser : public TestFixture {
33893393
ASSERT_EQUALS(true, settings->debugsymdb);
33903394
}
33913395

3396+
void safety() {
3397+
REDIRECT;
3398+
const char * const argv[] = {"cppcheck", "--safety", "file.cpp"};
3399+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3400+
ASSERT_EQUALS(true, settings->safety);
3401+
}
3402+
3403+
void safetyOverride() {
3404+
REDIRECT;
3405+
const char * const argv[] = {"cppcheck", "--no-safety", "--safety", "file.cpp"};
3406+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3407+
ASSERT_EQUALS(true, settings->safety);
3408+
}
3409+
3410+
void noSafety() {
3411+
REDIRECT;
3412+
const char * const argv[] = {"cppcheck", "--no-safety", "file.cpp"};
3413+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3414+
ASSERT_EQUALS(false, settings->safety);
3415+
}
3416+
3417+
void noSafetyOverride() {
3418+
REDIRECT;
3419+
const char * const argv[] = {"cppcheck", "--safety", "--no-safety", "file.cpp"};
3420+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3421+
ASSERT_EQUALS(false, settings->safety);
3422+
}
3423+
33923424
void ignorepaths1() {
33933425
REDIRECT;
33943426
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

test/testsettings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class TestSettings : public TestFixture {
250250
Suppressions supprs;
251251
ScopedFile file("cppcheck.cfg", "{\"safety\": false}");
252252
ASSERT_EQUALS("", Settings::loadCppcheckCfg(s, supprs));
253-
ASSERT_EQUALS(true, s.safety);
253+
ASSERT_EQUALS(false, s.safety);
254254
}
255255

256256
{

0 commit comments

Comments
 (0)