Skip to content

Commit 9f7a6bb

Browse files
committed
fixed safety override
1 parent 5103ea1 commit 9f7a6bb

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 3 additions & 0 deletions
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);

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
@@ -476,6 +476,10 @@ class TestCmdlineParser : public TestFixture {
476476
TEST_CASE(debugNormalVerbose);
477477
TEST_CASE(debug);
478478
TEST_CASE(debugVerbose);
479+
TEST_CASE(safety);
480+
TEST_CASE(safetyOverride);
481+
TEST_CASE(noSafety);
482+
TEST_CASE(noSafetyOverride);
479483

480484
TEST_CASE(ignorepaths1);
481485
TEST_CASE(ignorepaths2);
@@ -3320,6 +3324,34 @@ class TestCmdlineParser : public TestFixture {
33203324
ASSERT_EQUALS(true, settings->debugsymdb);
33213325
}
33223326

3327+
void safety() {
3328+
REDIRECT;
3329+
const char * const argv[] = {"cppcheck", "--safety", "file.cpp"};
3330+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3331+
ASSERT_EQUALS(true, settings->safety);
3332+
}
3333+
3334+
void safetyOverride() {
3335+
REDIRECT;
3336+
const char * const argv[] = {"cppcheck", "--no-safety", "--safety", "file.cpp"};
3337+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3338+
ASSERT_EQUALS(true, settings->safety);
3339+
}
3340+
3341+
void noSafety() {
3342+
REDIRECT;
3343+
const char * const argv[] = {"cppcheck", "--no-safety", "file.cpp"};
3344+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3345+
ASSERT_EQUALS(false, settings->safety);
3346+
}
3347+
3348+
void noSafetyOverride() {
3349+
REDIRECT;
3350+
const char * const argv[] = {"cppcheck", "--safety", "--no-safety", "file.cpp"};
3351+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3352+
ASSERT_EQUALS(false, settings->safety);
3353+
}
3354+
33233355
void ignorepaths1() {
33243356
REDIRECT;
33253357
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)