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