26
26
#include " fixture.h"
27
27
#include " helpers.h"
28
28
#include " threadexecutor.h"
29
+ #include " singleexecutor.h"
29
30
30
31
#include < algorithm>
31
32
#include < cstddef>
@@ -178,35 +179,35 @@ class TestSuppressions : public TestFixture {
178
179
}
179
180
180
181
// 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) {
182
183
// Clear the error log
183
184
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
+ }
184
191
185
192
CppCheck cppCheck (*this , true , nullptr );
186
193
Settings& settings = cppCheck.settings ();
187
- settings.exitCode = 1 ;
194
+ settings.jobs = 1 ;
188
195
settings.inlineSuppressions = true ;
196
+ settings.severity .enable (Severity::information);
189
197
if (suppression == " unusedFunction" )
190
198
settings.checks .setEnabled (Checks::unusedFunction, true );
191
- settings.severity .enable (Severity::information);
192
- settings.jobs = 1 ;
193
199
if (!suppression.empty ()) {
194
- std::string r = settings.nomsg .addSuppressionLine (suppression);
195
- EXPECT_EQ (" " , r);
200
+ EXPECT_EQ (" " , settings.nomsg .addSuppressionLine (suppression));
196
201
}
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 ));
197
207
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 ();
208
209
209
- CppCheckExecutor::reportSuppressions (settings, false , files_for_report , *this );
210
+ CppCheckExecutor::reportSuppressions (settings, false , files , *this );
210
211
211
212
return exitCode;
212
213
}
@@ -219,7 +220,7 @@ class TestSuppressions : public TestFixture {
219
220
files[" test.cpp" ] = strlen (code);
220
221
221
222
Settings settings;
222
- settings.jobs = 1 ;
223
+ settings.jobs = 2 ;
223
224
settings.inlineSuppressions = true ;
224
225
settings.severity .enable (Severity::information);
225
226
if (!suppression.empty ()) {
@@ -247,7 +248,7 @@ class TestSuppressions : public TestFixture {
247
248
files[" test.cpp" ] = strlen (code);
248
249
249
250
Settings settings;
250
- settings.jobs = 1 ;
251
+ settings.jobs = 2 ;
251
252
settings.inlineSuppressions = true ;
252
253
settings.severity .enable (Severity::information);
253
254
if (!suppression.empty ()) {
@@ -298,9 +299,8 @@ class TestSuppressions : public TestFixture {
298
299
" a++;\n "
299
300
" }\n " ,
300
301
" uninitvar:test.cpp" ));
301
- // TODO_ASSERT_EQUALS("", "[test.cpp]: (information) Unmatched suppression: uninitvar\n ", errout.str());
302
+ ASSERT_EQUALS ( " " , errout.str ());
302
303
303
- // TODO: add assert - gives different result with threads/processes
304
304
// suppress uninitvar for this file only, without error present
305
305
(this ->*check)(" void f() {\n "
306
306
" int a;\n "
@@ -315,9 +315,8 @@ class TestSuppressions : public TestFixture {
315
315
" a++;\n "
316
316
" }\n " ,
317
317
" *:test.cpp" ));
318
- // TODO_ASSERT_EQUALS("", "[test.cpp]: (information) Unmatched suppression: *\n ", errout.str());
318
+ ASSERT_EQUALS ( " " , errout.str ());
319
319
320
- // TODO: add assert - gives different result with threads/processes
321
320
// suppress all for this file only, without error present
322
321
(this ->*check)(" void f() {\n "
323
322
" int a;\n "
@@ -334,14 +333,13 @@ class TestSuppressions : public TestFixture {
334
333
" uninitvar:test.cpp:3" ));
335
334
ASSERT_EQUALS (" " , errout.str ());
336
335
337
- // TODO: add assert - gives different result with threads/processes
338
336
// suppress uninitvar for this file and line, without error present
339
337
(this ->*check)(" void f() {\n "
340
338
" int a;\n "
341
339
" b++;\n "
342
340
" }\n " ,
343
341
" 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 ());
345
343
346
344
// suppress uninitvar inline
347
345
ASSERT_EQUALS (0 , (this ->*check)(" void f() {\n "
@@ -464,15 +462,14 @@ class TestSuppressions : public TestFixture {
464
462
" " ));
465
463
ASSERT_EQUALS (" " , errout.str ());
466
464
467
- // TODO: add assert - gives different result with threads/processes
468
465
// suppress uninitvar inline, without error present
469
466
(this ->*check)(" void f() {\n "
470
467
" int a;\n "
471
468
" // cppcheck-suppress uninitvar\n "
472
469
" b++;\n "
473
470
" }\n " ,
474
471
" " );
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 ());
476
473
477
474
// #5746 - exitcode
478
475
ASSERT_EQUALS (1U ,
@@ -745,16 +742,14 @@ class TestSuppressions : public TestFixture {
745
742
}
746
743
747
744
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 " ;
750
746
751
- ASSERT_EQUALS (0 , checkSuppression (files , " syntaxError:test.cpp:1" ));
747
+ ASSERT_EQUALS (0 , checkSuppression (code , " syntaxError:test.cpp:1" ));
752
748
ASSERT_EQUALS (" " , errout.str ());
753
749
}
754
750
755
751
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 "
758
753
" _asm\n "
759
754
" {\n "
760
755
" // cppcheck-suppress syntaxError\n "
@@ -764,13 +759,12 @@ class TestSuppressions : public TestFixture {
764
759
" fstp QWORD PTR result ; store a double (8 bytes)\n "
765
760
" pop EAX ; restore EAX\n "
766
761
" }" ;
767
- ASSERT_EQUALS (0 , checkSuppression (files , " " ));
762
+ ASSERT_EQUALS (0 , checkSuppression (code , " " ));
768
763
ASSERT_EQUALS (" " , errout.str ());
769
764
}
770
765
771
766
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 "
774
768
" {\n "
775
769
" [!VAR \" BC\" = \" $BC + 1\" !][!//\n "
776
770
" [!IF \" (as:modconf('Ks')[1]/KsGeneral/KsType = 'KS_CFG_TYPE_KS_MASTER') and\n "
@@ -783,7 +777,7 @@ class TestSuppressions : public TestFixture {
783
777
" [!VAR \" BC\" = \" $BC + 1\" !][!//\n "
784
778
" [!ENDIF!][!//\n "
785
779
" };" ;
786
- ASSERT_EQUALS (0 , checkSuppression (files , " syntaxError:test.cpp:4" ));
780
+ ASSERT_EQUALS (0 , checkSuppression (code , " syntaxError:test.cpp:4" ));
787
781
ASSERT_EQUALS (" " , errout.str ());
788
782
}
789
783
@@ -813,34 +807,31 @@ class TestSuppressions : public TestFixture {
813
807
}
814
808
815
809
void suppressingSyntaxErrorAndExitCode () {
816
- std::map<std::string, std::string> files;
817
- files[" test.cpp" ] = " fi if;" ;
810
+ const char code[] = " fi if;" ;
818
811
819
- ASSERT_EQUALS (0 , checkSuppression (files , " *:test.cpp" ));
812
+ ASSERT_EQUALS (0 , checkSuppression (code , " *:test.cpp" ));
820
813
ASSERT_EQUALS (" " , errout.str ());
821
814
822
815
// multi files, but only suppression one
823
816
std::map<std::string, std::string> mfiles;
824
817
mfiles[" test.cpp" ] = " fi if;" ;
825
818
mfiles[" test2.cpp" ] = " fi if" ;
826
- ASSERT_EQUALS (1 , checkSuppression (mfiles, " *:test.cpp" ));
819
+ ASSERT_EQUALS (2 , checkSuppression (mfiles, " *:test.cpp" ));
827
820
ASSERT_EQUALS (" [test2.cpp:1]: (error) syntax error\n " , errout.str ());
828
821
829
822
// 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
834
826
ASSERT_EQUALS (" [test.cpp:2]: (error) syntax error\n " , errout.str ());
835
827
836
828
// 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
844
835
}
845
836
846
837
};
0 commit comments