Skip to content

Commit 14e540a

Browse files
authored
Fix #12341 (If --premium=safety is used then go to "safety mode". do not override this in cppcheck.cfg) (danmar#5872)
1 parent 3fb85f9 commit 14e540a

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

cli/cmdlineparser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
890890

891891
// Special Cppcheck Premium options
892892
else if (std::strncmp(argv[i], "--premium=", 10) == 0 && isCppcheckPremium()) {
893+
if (std::strcmp(argv[i], "--premium=safety") == 0)
894+
mSettings.safety = true;
893895
if (!mSettings.premiumArgs.empty())
894896
mSettings.premiumArgs += " ";
895897
const std::string p(argv[i] + 10);

lib/settings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ std::string Settings::loadCppcheckCfg()
125125
const auto& v = it->second;
126126
if (!v.is<bool>())
127127
return "'safety' is not a bool";
128-
safety = v.get<bool>();
128+
safety = safety || v.get<bool>();
129129
}
130130
}
131131

test/testcmdlineparser.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ class TestCmdlineParser : public TestFixture {
213213
TEST_CASE(maxConfigsMissingCount);
214214
TEST_CASE(maxConfigsInvalid);
215215
TEST_CASE(maxConfigsTooSmall);
216+
TEST_CASE(premiumSafety);
216217
TEST_CASE(reportProgress1);
217218
TEST_CASE(reportProgress2);
218219
TEST_CASE(reportProgress3);
@@ -1186,6 +1187,17 @@ class TestCmdlineParser : public TestFixture {
11861187
ASSERT_EQUALS("cppcheck: error: argument to '--max-configs=' must be greater than 0.\n", logger->str());
11871188
}
11881189

1190+
void premiumSafety() {
1191+
REDIRECT;
1192+
const char * const argv[] = {"cppcheck", "--premium=safety", "file.cpp"};
1193+
settings->safety = false;
1194+
settings->cppcheckCfgProductName = "Cppcheck Premium 0.0.0";
1195+
ASSERT_EQUALS(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
1196+
ASSERT_EQUALS(true, settings->safety);
1197+
settings->safety = false;
1198+
settings->cppcheckCfgProductName.clear();
1199+
}
1200+
11891201
void reportProgress1() {
11901202
REDIRECT;
11911203
const char * const argv[] = {"cppcheck", "--report-progress", "file.cpp"};

test/testsettings.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TestSettings : public TestFixture {
3030
void run() override {
3131
TEST_CASE(simpleEnableGroup);
3232
TEST_CASE(loadCppcheckCfg);
33+
TEST_CASE(loadCppcheckCfgSafety);
3334
}
3435

3536
void simpleEnableGroup() const {
@@ -209,6 +210,34 @@ class TestSettings : public TestFixture {
209210

210211
// TODO: test with FILESDIR
211212
}
213+
214+
void loadCppcheckCfgSafety() const
215+
{
216+
// Test the "safety" flag
217+
{
218+
Settings s;
219+
s.safety = false;
220+
ScopedFile file("cppcheck.cfg", "{}");
221+
ASSERT_EQUALS("", s.loadCppcheckCfg());
222+
ASSERT_EQUALS(false, s.safety);
223+
}
224+
225+
{
226+
Settings s;
227+
s.safety = true;
228+
ScopedFile file("cppcheck.cfg", "{\"safety\": false}");
229+
ASSERT_EQUALS("", s.loadCppcheckCfg());
230+
ASSERT_EQUALS(true, s.safety);
231+
}
232+
233+
{
234+
Settings s;
235+
s.safety = false;
236+
ScopedFile file("cppcheck.cfg", "{\"safety\": true}");
237+
ASSERT_EQUALS("", s.loadCppcheckCfg());
238+
ASSERT_EQUALS(true, s.safety);
239+
}
240+
}
212241
};
213242

214243
REGISTER_TEST(TestSettings)

0 commit comments

Comments
 (0)