Skip to content

Commit 362cb54

Browse files
committed
allow safety from config to be overwritten
1 parent b65e45d commit 362cb54

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
@@ -1010,6 +1010,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10101010
mSettings.cppHeaderProbe = false;
10111011
}
10121012

1013+
else if (std::strcmp(argv[i], "--no-safety") == 0)
1014+
mSettings.safety = false;
1015+
10131016
// Write results in file
10141017
else if (std::strncmp(argv[i], "--output-file=", 14) == 0)
10151018
mSettings.outputFile = Path::simplifyPath(argv[i] + 14);
@@ -1110,7 +1113,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11101113
"misra-c++-2023",
11111114
"misra-cpp-2023",
11121115
"bughunting",
1113-
"safety",
1116+
"safety", // TODO: deprecate in favor of the regular --saftey/--no-safety
11141117
"debug-progress"};
11151118
// valid options --premium-..=
11161119
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
@@ -1722,6 +1722,27 @@ def test_config_invalid(tmpdir):
17221722
]
17231723

17241724

1725+
def test_config_override(tmpdir):
1726+
# cppcheck.cfg needs to be next to executable
1727+
exe = shutil.copy2(__lookup_cppcheck_exe(), tmpdir)
1728+
shutil.copytree(os.path.join(os.path.dirname(__lookup_cppcheck_exe()), 'cfg'), os.path.join(tmpdir, 'cfg'))
1729+
1730+
test_file = os.path.join(tmpdir, 'test.c')
1731+
with open(test_file, 'wt'):
1732+
pass
1733+
1734+
config_file = os.path.join(tmpdir, 'cppcheck.cfg')
1735+
with open(config_file, 'wt') as f:
1736+
f.write(json.dumps({
1737+
'safety': False
1738+
}))
1739+
1740+
exitcode, stdout, stderr, exe = cppcheck_ex(['-q', '--safety', test_file], cwd=tmpdir, cppcheck_exe=exe, remove_checkers_report=False)
1741+
assert exitcode == 0, stdout if stdout else stderr
1742+
assert stdout.splitlines() == []
1743+
assert 'checkersReport' in stderr
1744+
1745+
17251746
def test_checkers_report(tmpdir):
17261747
test_file = os.path.join(tmpdir, 'test.c')
17271748
with open(test_file, 'wt') as f:

test/testcmdlineparser.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ class TestCmdlineParser : public TestFixture {
478478
TEST_CASE(debugNormalVerbose);
479479
TEST_CASE(debug);
480480
TEST_CASE(debugVerbose);
481+
TEST_CASE(safety);
482+
TEST_CASE(safetyOverride);
483+
TEST_CASE(noSafety);
484+
TEST_CASE(noSafetyOverride);
481485

482486
TEST_CASE(ignorepaths1);
483487
TEST_CASE(ignorepaths2);
@@ -3360,6 +3364,34 @@ class TestCmdlineParser : public TestFixture {
33603364
ASSERT_EQUALS(true, settings->debugsymdb);
33613365
}
33623366

3367+
void safety() {
3368+
REDIRECT;
3369+
const char * const argv[] = {"cppcheck", "--safety", "file.cpp"};
3370+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3371+
ASSERT_EQUALS(true, settings->safety);
3372+
}
3373+
3374+
void safetyOverride() {
3375+
REDIRECT;
3376+
const char * const argv[] = {"cppcheck", "--no-safety", "--safety", "file.cpp"};
3377+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3378+
ASSERT_EQUALS(true, settings->safety);
3379+
}
3380+
3381+
void noSafety() {
3382+
REDIRECT;
3383+
const char * const argv[] = {"cppcheck", "--no-safety", "file.cpp"};
3384+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3385+
ASSERT_EQUALS(false, settings->safety);
3386+
}
3387+
3388+
void noSafetyOverride() {
3389+
REDIRECT;
3390+
const char * const argv[] = {"cppcheck", "--safety", "--no-safety", "file.cpp"};
3391+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3392+
ASSERT_EQUALS(false, settings->safety);
3393+
}
3394+
33633395
void ignorepaths1() {
33643396
REDIRECT;
33653397
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)