Skip to content

Commit 2502897

Browse files
authored
avoid some redundant and unused settings in tests among other cleanups / added and used WARN_UNUSED attribute (danmar#5284)
1 parent 8166bfc commit 2502897

18 files changed

Lines changed: 85 additions & 55 deletions

lib/config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979
# define UNUSED
8080
#endif
8181

82+
// warn_unused
83+
#if (defined(__clang__) && (__clang_major__ >= 15))
84+
# define WARN_UNUSED [[gnu::warn_unused]]
85+
#else
86+
# define WARN_UNUSED
87+
#endif
88+
8289
#define REQUIRES(msg, ...) class=typename std::enable_if<__VA_ARGS__::value>::type
8390

8491
#include <string>

lib/settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class SimpleEnableGroup {
9090
* to pass individual values to functions or constructors now or in the
9191
* future when we might have even more detailed settings.
9292
*/
93-
class CPPCHECKLIB Settings {
93+
class CPPCHECKLIB WARN_UNUSED Settings {
9494
private:
9595

9696
/** @brief terminate checking */

test/fixture.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ void TestFixture::setTemplateFormat(const std::string &templateFormat)
399399
}
400400

401401
TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::library(const char lib[]) {
402+
if (REDUNDANT_CHECK && std::find(settings.libraries.cbegin(), settings.libraries.cend(), lib) != settings.libraries.cend())
403+
throw std::runtime_error("redundant setting: libraries (" + std::string(lib) + ")");
402404
// TODO: exename is not yet set
403405
LOAD_LIB_2_EXE(settings.library, lib, fixture.exename.c_str());
404406
// strip extension
@@ -414,6 +416,11 @@ TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::library(const char l
414416
TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::platform(cppcheck::Platform::Type type)
415417
{
416418
const std::string platformStr = cppcheck::Platform::toString(type);
419+
420+
// TODO: the default platform differs between Windows and Linux
421+
//if (REDUNDANT_CHECK && settings.platform.type == type)
422+
// throw std::runtime_error("redundant setting: platform (" + platformStr + ")");
423+
417424
std::string errstr;
418425
// TODO: exename is not yet set
419426
if (!settings.platform.set(platformStr, errstr, {fixture.exename}))

test/fixture.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,49 +130,66 @@ class TestFixture : public ErrorLogger {
130130
check.runChecks(tokenizer, settings, errorLogger);
131131
}
132132

133-
// TODO: bail out on redundant settings
134133
class SettingsBuilder
135134
{
136135
public:
137136
explicit SettingsBuilder(const TestFixture &fixture) : fixture(fixture) {}
138137
SettingsBuilder(const TestFixture &fixture, Settings settings) : fixture(fixture), settings(std::move(settings)) {}
139138

140139
SettingsBuilder& severity(Severity::SeverityType sev, bool b = true) {
140+
if (REDUNDANT_CHECK && settings.severity.isEnabled(sev) == b)
141+
throw std::runtime_error("redundant setting: severity");
141142
settings.severity.setEnabled(sev, b);
142143
return *this;
143144
}
144145

145146
SettingsBuilder& certainty(Certainty cert, bool b = true) {
147+
if (REDUNDANT_CHECK && settings.certainty.isEnabled(cert) == b)
148+
throw std::runtime_error("redundant setting: certainty");
146149
settings.certainty.setEnabled(cert, b);
147150
return *this;
148151
}
149152

150153
SettingsBuilder& clang() {
154+
if (REDUNDANT_CHECK && settings.clang)
155+
throw std::runtime_error("redundant setting: clang");
151156
settings.clang = true;
152157
return *this;
153158
}
154159

155160
SettingsBuilder& checkLibrary() {
161+
if (REDUNDANT_CHECK && settings.checkLibrary)
162+
throw std::runtime_error("redundant setting: checkLibrary");
156163
settings.checkLibrary = true;
157164
return *this;
158165
}
159166

160167
SettingsBuilder& checkUnusedTemplates(bool b = true) {
168+
if (REDUNDANT_CHECK && settings.checkUnusedTemplates == b)
169+
throw std::runtime_error("redundant setting: checkUnusedTemplates");
161170
settings.checkUnusedTemplates = b;
162171
return *this;
163172
}
164173

165174
SettingsBuilder& debugwarnings(bool b = true) {
175+
if (REDUNDANT_CHECK && settings.debugwarnings == b)
176+
throw std::runtime_error("redundant setting: debugwarnings");
166177
settings.debugwarnings = b;
167178
return *this;
168179
}
169180

170181
SettingsBuilder& c(Standards::cstd_t std) {
182+
// TODO: CLatest and C11 are the same - handle differently
183+
//if (REDUNDANT_CHECK && settings.standards.c == std)
184+
// throw std::runtime_error("redundant setting: standards.c");
171185
settings.standards.c = std;
172186
return *this;
173187
}
174188

175189
SettingsBuilder& cpp(Standards::cppstd_t std) {
190+
// TODO: CPPLatest and CPP20 are the same - handle differently
191+
//if (REDUNDANT_CHECK && settings.standards.cpp == std)
192+
// throw std::runtime_error("redundant setting: standards.cpp");
176193
settings.standards.cpp = std;
177194
return *this;
178195
}
@@ -184,11 +201,15 @@ class TestFixture : public ErrorLogger {
184201
SettingsBuilder& platform(cppcheck::Platform::Type type);
185202

186203
SettingsBuilder& checkConfiguration() {
204+
if (REDUNDANT_CHECK && settings.checkConfiguration)
205+
throw std::runtime_error("redundant setting: checkConfiguration");
187206
settings.checkConfiguration = true;
188207
return *this;
189208
}
190209

191210
SettingsBuilder& checkHeaders(bool b = true) {
211+
if (REDUNDANT_CHECK && settings.checkHeaders == b)
212+
throw std::runtime_error("redundant setting: checkHeaders");
192213
settings.checkHeaders = b;
193214
return *this;
194215
}
@@ -199,6 +220,8 @@ class TestFixture : public ErrorLogger {
199220
private:
200221
const TestFixture &fixture;
201222
Settings settings;
223+
224+
const bool REDUNDANT_CHECK = false;
202225
};
203226

204227
SettingsBuilder settingsBuilder() const {

test/testbufferoverrun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class TestBufferOverrun : public TestFixture {
7676
// Clear the error buffer..
7777
errout.str("");
7878

79-
const Settings settings = settingsBuilder(settings0).severity(Severity::style).severity(Severity::warning).severity(Severity::portability).severity(Severity::performance)
79+
const Settings settings = settingsBuilder(settings0).severity(Severity::performance)
8080
.c(Standards::CLatest).cpp(Standards::CPPLatest).certainty(Certainty::inconclusive).build();
8181

8282
// Raw tokens..

test/testclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class TestClass : public TestFixture {
3939

4040
private:
4141
Settings settings0 = settingsBuilder().severity(Severity::style).library("std.cfg").build();
42-
Settings settings1 = settingsBuilder().severity(Severity::warning).library("std.cfg").build();
42+
const Settings settings1 = settingsBuilder().severity(Severity::warning).library("std.cfg").build();
4343

4444
void run() override {
4545
TEST_CASE(virtualDestructor1); // Base class not found => no error

test/testcondition.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class TestCondition : public TestFixture {
127127
TEST_CASE(knownConditionIncrementLoop); // #9808
128128
}
129129

130-
void check(const char code[], Settings &settings, const char* filename = "test.cpp") {
130+
void check(const char code[], const Settings &settings, const char* filename = "test.cpp") {
131131
// Clear the error buffer..
132132
errout.str("");
133133

@@ -154,7 +154,7 @@ class TestCondition : public TestFixture {
154154
}
155155

156156
void check(const char code[], const char* filename = "test.cpp", bool inconclusive = false) {
157-
Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive, inconclusive).build();
157+
const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive, inconclusive).build();
158158
check(code, settings, filename);
159159
}
160160

@@ -5645,7 +5645,7 @@ class TestCondition : public TestFixture {
56455645
}
56465646

56475647
void compareOutOfTypeRange() {
5648-
Settings settingsUnix64 = settingsBuilder().severity(Severity::style).platform(cppcheck::Platform::Type::Unix64).build();
5648+
const Settings settingsUnix64 = settingsBuilder().severity(Severity::style).platform(cppcheck::Platform::Type::Unix64).build();
56495649

56505650
check("void f(unsigned char c) {\n"
56515651
" if (c == 256) {}\n"

test/testexceptionsafety.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class TestExceptionSafety : public TestFixture {
6363
// Clear the error buffer..
6464
errout.str("");
6565

66-
Settings settings1 = settingsBuilder(s ? *s : settings).certainty(Certainty::inconclusive, inconclusive).build();
66+
const Settings settings1 = settingsBuilder(s ? *s : settings).certainty(Certainty::inconclusive, inconclusive).build();
6767

6868
// Tokenize..
6969
Tokenizer tokenizer(&settings1, this);
@@ -397,7 +397,7 @@ class TestExceptionSafety : public TestFixture {
397397
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style, inconclusive) Unhandled exception specification when calling function f().\n"
398398
"[test.cpp:6] -> [test.cpp:1]: (style, inconclusive) Unhandled exception specification when calling function f().\n", errout.str());
399399

400-
const Settings s = settingsBuilder(settings).library("gnu.cfg").build();
400+
const Settings s = settingsBuilder().library("gnu.cfg").build();
401401
check(code, true, &s);
402402
ASSERT_EQUALS("", errout.str());
403403
}

test/testfunctions.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,18 +1562,21 @@ class TestFunctions : public TestFixture {
15621562

15631563
{
15641564
const char code[] = "int main(void) {}";
1565-
Settings s;
1565+
{
1566+
const Settings s = settingsBuilder().c(Standards::C89).build();
15661567

1567-
s.standards.c = Standards::C89;
1568-
check(code, "test.c", &s); // c code (c89)
1569-
ASSERT_EQUALS("[test.c:1]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout.str());
1568+
check(code, "test.c", &s); // c code (c89)
1569+
ASSERT_EQUALS("[test.c:1]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout.str());
1570+
}
15701571

1571-
s.standards.c = Standards::C99;
1572-
check(code, "test.c", &s); // c code (c99)
1573-
ASSERT_EQUALS("", errout.str());
1572+
{
1573+
const Settings s = settingsBuilder().c(Standards::C99).build();
1574+
check(code, "test.c", &s); // c code (c99)
1575+
ASSERT_EQUALS("", errout.str());
15741576

1575-
check(code, "test.cpp", &s); // c++ code
1576-
ASSERT_EQUALS("", errout.str());
1577+
check(code, "test.cpp", &s); // c++ code
1578+
ASSERT_EQUALS("", errout.str());
1579+
}
15771580
}
15781581

15791582
check("F(A,B) { x=1; }");
@@ -1825,9 +1828,8 @@ class TestFunctions : public TestFixture {
18251828
}
18261829

18271830
void checkLibraryMatchFunctions() {
1828-
Settings s = settingsBuilder(settings).checkLibrary().build();
1831+
Settings s = settingsBuilder(settings).checkLibrary().debugwarnings().build();
18291832
s.daca = true;
1830-
s.debugwarnings = true;
18311833

18321834
check("void f() {\n"
18331835
" lib_func();"

test/testleakautovar.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ class TestLeakAutoVar : public TestFixture {
474474
}
475475

476476
void assign23() {
477-
const Settings s = settingsBuilder(settings).library("posix.cfg").build();
477+
const Settings s = settingsBuilder().library("posix.cfg").build();
478478
check("void f() {\n"
479479
" int n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14;\n"
480480
" *&n1 = open(\"xx.log\", O_RDONLY);\n"
@@ -2764,8 +2764,6 @@ class TestLeakAutoVar : public TestFixture {
27642764
}
27652765

27662766
void functionCallCastConfig() { // #9652
2767-
Settings settingsFunctionCall = settings;
2768-
27692767
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
27702768
"<def format=\"2\">\n"
27712769
" <function name=\"free_func\">\n"
@@ -2778,7 +2776,8 @@ class TestLeakAutoVar : public TestFixture {
27782776
" </arg>\n"
27792777
" </function>\n"
27802778
"</def>";
2781-
ASSERT(settingsFunctionCall.library.loadxmldata(xmldata, sizeof(xmldata)));
2779+
const Settings settingsFunctionCall = settingsBuilder(settings).libraryxml(xmldata, sizeof(xmldata)).build();
2780+
27822781
check("void test_func()\n"
27832782
"{\n"
27842783
" char * buf = malloc(4);\n"
@@ -2810,7 +2809,7 @@ class TestLeakAutoVar : public TestFixture {
28102809
" <arg nr=\"1\" direction=\"in\"/>\n"
28112810
" </function>\n"
28122811
"</def>\n";
2813-
const Settings settingsLeakIgnore = settingsBuilder(settings).libraryxml(xmldata, sizeof(xmldata)).build();
2812+
const Settings settingsLeakIgnore = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
28142813
check("void f() {\n"
28152814
" double* a = new double[1024];\n"
28162815
" SomeClass::someMethod(a);\n"

0 commit comments

Comments
 (0)