Skip to content

Commit c450ae1

Browse files
committed
fixed safety override
1 parent d7469cd commit c450ae1

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
@@ -1019,6 +1019,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10191019
mSettings.cppHeaderProbe = false;
10201020
}
10211021

1022+
else if (std::strcmp(argv[i], "--no-safety") == 0)
1023+
mSettings.safety = false;
1024+
10221025
// Write results in file
10231026
else if (std::strncmp(argv[i], "--output-file=", 14) == 0)
10241027
mSettings.outputFile = Path::simplifyPath(argv[i] + 14);
@@ -1119,7 +1122,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11191122
"misra-c++-2023",
11201123
"misra-cpp-2023",
11211124
"bughunting",
1122-
"safety",
1125+
"safety", // TODO: deprecate in favor of the -regular -saftey/--no-safety
11231126
"debug-progress"};
11241127
// valid options --premium-..=
11251128
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 stderr.splitlines() == []
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
@@ -479,6 +479,10 @@ class TestCmdlineParser : public TestFixture {
479479
TEST_CASE(debugNormalVerbose);
480480
TEST_CASE(debug);
481481
TEST_CASE(debugVerbose);
482+
TEST_CASE(safety);
483+
TEST_CASE(safetyOverride);
484+
TEST_CASE(noSafety);
485+
TEST_CASE(noSafetyOverride);
482486

483487
TEST_CASE(ignorepaths1);
484488
TEST_CASE(ignorepaths2);
@@ -3357,6 +3361,34 @@ class TestCmdlineParser : public TestFixture {
33573361
ASSERT_EQUALS(true, settings->debugsymdb);
33583362
}
33593363

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