Skip to content

Commit 6eae4e7

Browse files
authored
make sure executors are only used in the intended context / TestSuppressions cleanups (#4963)
* added asserts to make sure executors are only used in the intended context * TestSuppressions: specify proper job counts in `checkSuppression*()` * TestSuppressions: enabled all asserts in `runChecks()` * TestSuppressions: removed unnecessary setting from `checkSuppression()` * TestSuppressions: small cleanup in the way tests are called * TestSuppressions: use `SingleExecutor`
1 parent 5658319 commit 6eae4e7

File tree

5 files changed

+55
-55
lines changed

5 files changed

+55
-55
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ test/teststring.o: test/teststring.cpp lib/check.h lib/checkstring.h lib/color.h
823823
test/testsummaries.o: test/testsummaries.cpp lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h
824824
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsummaries.cpp
825825

826-
test/testsuppressions.o: test/testsuppressions.cpp cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
826+
test/testsuppressions.o: test/testsuppressions.cpp cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/singleexecutor.h cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
827827
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsuppressions.cpp
828828

829829
test/testsymboldatabase.o: test/testsymboldatabase.cpp lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h

cli/processexecutor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <algorithm>
3434
#include <numeric>
35+
#include <cassert>
3536
#include <cerrno>
3637
#include <csignal>
3738
#include <cstdlib>
@@ -61,7 +62,9 @@ using std::memset;
6162

6263
ProcessExecutor::ProcessExecutor(const std::map<std::string, std::size_t> &files, Settings &settings, ErrorLogger &errorLogger)
6364
: Executor(files, settings, errorLogger)
64-
{}
65+
{
66+
assert(mSettings.jobs > 1);
67+
}
6568

6669
ProcessExecutor::~ProcessExecutor()
6770
{}

cli/singleexecutor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "library.h"
2424
#include "settings.h"
2525

26+
#include <cassert>
2627
#include <list>
2728
#include <numeric>
2829
#include <utility>
@@ -32,7 +33,9 @@ class ErrorLogger;
3233
SingleExecutor::SingleExecutor(CppCheck &cppcheck, const std::map<std::string, std::size_t> &files, Settings &settings, ErrorLogger &errorLogger)
3334
: Executor(files, settings, errorLogger)
3435
, mCppcheck(cppcheck)
35-
{}
36+
{
37+
assert(mSettings.jobs == 1);
38+
}
3639

3740
SingleExecutor::~SingleExecutor()
3841
{}

cli/threadexecutor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "settings.h"
2727

2828
#include <algorithm>
29+
#include <cassert>
2930
#include <cstdlib>
3031
#include <functional>
3132
#include <future>
@@ -41,7 +42,9 @@ enum class Color;
4142

4243
ThreadExecutor::ThreadExecutor(const std::map<std::string, std::size_t> &files, Settings &settings, ErrorLogger &errorLogger)
4344
: Executor(files, settings, errorLogger)
44-
{}
45+
{
46+
assert(mSettings.jobs > 1);
47+
}
4548

4649
ThreadExecutor::~ThreadExecutor()
4750
{}

test/testsuppressions.cpp

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "fixture.h"
2727
#include "helpers.h"
2828
#include "threadexecutor.h"
29+
#include "singleexecutor.h"
2930

3031
#include <algorithm>
3132
#include <cstddef>
@@ -178,35 +179,35 @@ class TestSuppressions : public TestFixture {
178179
}
179180

180181
// Check the suppression for multiple files
181-
unsigned int checkSuppression(std::map<std::string, std::string> &files, const std::string &suppression = emptyString) {
182+
unsigned int checkSuppression(std::map<std::string, std::string> &f, const std::string &suppression = emptyString) {
182183
// Clear the error log
183184
errout.str("");
185+
output.str("");
186+
187+
std::map<std::string, std::size_t> files;
188+
for (std::map<std::string, std::string>::const_iterator i = f.cbegin(); i != f.cend(); ++i) {
189+
files[i->first] = i->second.size();
190+
}
184191

185192
CppCheck cppCheck(*this, true, nullptr);
186193
Settings& settings = cppCheck.settings();
187-
settings.exitCode = 1;
194+
settings.jobs = 1;
188195
settings.inlineSuppressions = true;
196+
settings.severity.enable(Severity::information);
189197
if (suppression == "unusedFunction")
190198
settings.checks.setEnabled(Checks::unusedFunction, true);
191-
settings.severity.enable(Severity::information);
192-
settings.jobs = 1;
193199
if (!suppression.empty()) {
194-
std::string r = settings.nomsg.addSuppressionLine(suppression);
195-
EXPECT_EQ("", r);
200+
EXPECT_EQ("", settings.nomsg.addSuppressionLine(suppression));
196201
}
202+
SingleExecutor executor(cppCheck, files, settings, *this);
203+
std::vector<std::unique_ptr<ScopedFile>> scopedfiles;
204+
scopedfiles.reserve(files.size());
205+
for (std::map<std::string, std::string>::const_iterator i = f.cbegin(); i != f.cend(); ++i)
206+
scopedfiles.emplace_back(new ScopedFile(i->first, i->second));
197207

198-
unsigned int exitCode = std::accumulate(files.cbegin(), files.cend(), 0U, [&](unsigned int v, const std::pair<std::string, std::string>& f) {
199-
return v | cppCheck.check(f.first, f.second);
200-
});
201-
202-
if (cppCheck.analyseWholeProgram())
203-
exitCode |= settings.exitCode;
204-
205-
std::map<std::string, std::size_t> files_for_report;
206-
for (std::map<std::string, std::string>::const_iterator file = files.cbegin(); file != files.cend(); ++file)
207-
files_for_report[file->first] = file->second.size();
208+
const unsigned int exitCode = executor.check();
208209

209-
CppCheckExecutor::reportSuppressions(settings, false, files_for_report, *this);
210+
CppCheckExecutor::reportSuppressions(settings, false, files, *this);
210211

211212
return exitCode;
212213
}
@@ -219,7 +220,7 @@ class TestSuppressions : public TestFixture {
219220
files["test.cpp"] = strlen(code);
220221

221222
Settings settings;
222-
settings.jobs = 1;
223+
settings.jobs = 2;
223224
settings.inlineSuppressions = true;
224225
settings.severity.enable(Severity::information);
225226
if (!suppression.empty()) {
@@ -247,7 +248,7 @@ class TestSuppressions : public TestFixture {
247248
files["test.cpp"] = strlen(code);
248249

249250
Settings settings;
250-
settings.jobs = 1;
251+
settings.jobs = 2;
251252
settings.inlineSuppressions = true;
252253
settings.severity.enable(Severity::information);
253254
if (!suppression.empty()) {
@@ -298,9 +299,8 @@ class TestSuppressions : public TestFixture {
298299
" a++;\n"
299300
"}\n",
300301
"uninitvar:test.cpp"));
301-
//TODO_ASSERT_EQUALS("", "[test.cpp]: (information) Unmatched suppression: uninitvar\n", errout.str());
302+
ASSERT_EQUALS("", errout.str());
302303

303-
// TODO: add assert - gives different result with threads/processes
304304
// suppress uninitvar for this file only, without error present
305305
(this->*check)("void f() {\n"
306306
" int a;\n"
@@ -315,9 +315,8 @@ class TestSuppressions : public TestFixture {
315315
" a++;\n"
316316
"}\n",
317317
"*:test.cpp"));
318-
//TODO_ASSERT_EQUALS("", "[test.cpp]: (information) Unmatched suppression: *\n", errout.str());
318+
ASSERT_EQUALS("", errout.str());
319319

320-
// TODO: add assert - gives different result with threads/processes
321320
// suppress all for this file only, without error present
322321
(this->*check)("void f() {\n"
323322
" int a;\n"
@@ -334,14 +333,13 @@ class TestSuppressions : public TestFixture {
334333
"uninitvar:test.cpp:3"));
335334
ASSERT_EQUALS("", errout.str());
336335

337-
// TODO: add assert - gives different result with threads/processes
338336
// suppress uninitvar for this file and line, without error present
339337
(this->*check)("void f() {\n"
340338
" int a;\n"
341339
" b++;\n"
342340
"}\n",
343341
"uninitvar:test.cpp:3");
344-
//TODO_ASSERT_EQUALS("[test.cpp:3]: (information) Unmatched suppression: uninitvar\n", "", errout.str());
342+
ASSERT_EQUALS("[test.cpp:3]: (information) Unmatched suppression: uninitvar\n", errout.str());
345343

346344
// suppress uninitvar inline
347345
ASSERT_EQUALS(0, (this->*check)("void f() {\n"
@@ -464,15 +462,14 @@ class TestSuppressions : public TestFixture {
464462
""));
465463
ASSERT_EQUALS("", errout.str());
466464

467-
// TODO: add assert - gives different result with threads/processes
468465
// suppress uninitvar inline, without error present
469466
(this->*check)("void f() {\n"
470467
" int a;\n"
471468
" // cppcheck-suppress uninitvar\n"
472469
" b++;\n"
473470
"}\n",
474471
"");
475-
//TODO_ASSERT_EQUALS("[test.cpp:4]: (information) Unmatched suppression: uninitvar\n", "", errout.str());
472+
ASSERT_EQUALS("[test.cpp:4]: (information) Unmatched suppression: uninitvar\n", errout.str());
476473

477474
// #5746 - exitcode
478475
ASSERT_EQUALS(1U,
@@ -745,16 +742,14 @@ class TestSuppressions : public TestFixture {
745742
}
746743

747744
void suppressingSyntaxErrors() { // syntaxErrors should be suppressible (#7076)
748-
std::map<std::string, std::string> files;
749-
files["test.cpp"] = "if if\n";
745+
const char code[] = "if if\n";
750746

751-
ASSERT_EQUALS(0, checkSuppression(files, "syntaxError:test.cpp:1"));
747+
ASSERT_EQUALS(0, checkSuppression(code, "syntaxError:test.cpp:1"));
752748
ASSERT_EQUALS("", errout.str());
753749
}
754750

755751
void suppressingSyntaxErrorsInline() { // syntaxErrors should be suppressible (#5917)
756-
std::map<std::string, std::string> files;
757-
files["test.cpp"] = "double result(0.0);\n"
752+
const char code[] = "double result(0.0);\n"
758753
"_asm\n"
759754
"{\n"
760755
" // cppcheck-suppress syntaxError\n"
@@ -764,13 +759,12 @@ class TestSuppressions : public TestFixture {
764759
" fstp QWORD PTR result ; store a double (8 bytes)\n"
765760
" pop EAX ; restore EAX\n"
766761
"}";
767-
ASSERT_EQUALS(0, checkSuppression(files, ""));
762+
ASSERT_EQUALS(0, checkSuppression(code, ""));
768763
ASSERT_EQUALS("", errout.str());
769764
}
770765

771766
void suppressingSyntaxErrorsWhileFileRead() { // syntaxError while file read should be suppressible (PR #1333)
772-
std::map<std::string, std::string> files;
773-
files["test.cpp"] = "CONST (genType, KS_CONST) genService[KS_CFG_NR_OF_NVM_BLOCKS] =\n"
767+
const char code[] = "CONST (genType, KS_CONST) genService[KS_CFG_NR_OF_NVM_BLOCKS] =\n"
774768
"{\n"
775769
"[!VAR \"BC\" = \"$BC + 1\"!][!//\n"
776770
"[!IF \"(as:modconf('Ks')[1]/KsGeneral/KsType = 'KS_CFG_TYPE_KS_MASTER') and\n"
@@ -783,7 +777,7 @@ class TestSuppressions : public TestFixture {
783777
"[!VAR \"BC\" = \"$BC + 1\"!][!//\n"
784778
"[!ENDIF!][!//\n"
785779
"};";
786-
ASSERT_EQUALS(0, checkSuppression(files, "syntaxError:test.cpp:4"));
780+
ASSERT_EQUALS(0, checkSuppression(code, "syntaxError:test.cpp:4"));
787781
ASSERT_EQUALS("", errout.str());
788782
}
789783

@@ -813,34 +807,31 @@ class TestSuppressions : public TestFixture {
813807
}
814808

815809
void suppressingSyntaxErrorAndExitCode() {
816-
std::map<std::string, std::string> files;
817-
files["test.cpp"] = "fi if;";
810+
const char code[] = "fi if;";
818811

819-
ASSERT_EQUALS(0, checkSuppression(files, "*:test.cpp"));
812+
ASSERT_EQUALS(0, checkSuppression(code, "*:test.cpp"));
820813
ASSERT_EQUALS("", errout.str());
821814

822815
// multi files, but only suppression one
823816
std::map<std::string, std::string> mfiles;
824817
mfiles["test.cpp"] = "fi if;";
825818
mfiles["test2.cpp"] = "fi if";
826-
ASSERT_EQUALS(1, checkSuppression(mfiles, "*:test.cpp"));
819+
ASSERT_EQUALS(2, checkSuppression(mfiles, "*:test.cpp"));
827820
ASSERT_EQUALS("[test2.cpp:1]: (error) syntax error\n", errout.str());
828821

829822
// multi error in file, but only suppression one error
830-
std::map<std::string, std::string> file2;
831-
file2["test.cpp"] = "fi fi\n"
832-
"if if;";
833-
ASSERT_EQUALS(1, checkSuppression(file2, "*:test.cpp:1")); // suppress all error at line 1 of test.cpp
823+
const char code2[] = "fi fi\n"
824+
"if if;";
825+
ASSERT_EQUALS(2, checkSuppression(code2, "*:test.cpp:1")); // suppress all error at line 1 of test.cpp
834826
ASSERT_EQUALS("[test.cpp:2]: (error) syntax error\n", errout.str());
835827

836828
// multi error in file, but only suppression one error (2)
837-
std::map<std::string, std::string> file3;
838-
file3["test.cpp"] = "void f(int x, int y){\n"
839-
" int a = x/0;\n"
840-
" int b = y/0;\n"
841-
"}\n"
842-
"f(0, 1);\n";
843-
ASSERT_EQUALS(1, checkSuppression(file3, "zerodiv:test.cpp:3")); // suppress 'errordiv' at line 3 of test.cpp
829+
const char code3[] = "void f(int x, int y){\n"
830+
" int a = x/0;\n"
831+
" int b = y/0;\n"
832+
"}\n"
833+
"f(0, 1);\n";
834+
ASSERT_EQUALS(2, checkSuppression(code3, "zerodiv:test.cpp:3")); // suppress 'errordiv' at line 3 of test.cpp
844835
}
845836

846837
};

0 commit comments

Comments
 (0)