From 3617c11fb9ff6a18797f8cfa5e77d91daeef1caf Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 17 Jan 2024 23:33:58 +0100 Subject: [PATCH] properly reset `CheckUnusedFunctions` data in tests / made `CheckUnusedFunctions::clear()` private --- Makefile | 10 +++++----- cli/cppcheckexecutor.cpp | 4 ---- lib/checkunusedfunctions.h | 8 ++++++-- test/testprocessexecutor.cpp | 3 +++ test/testsingleexecutor.cpp | 3 +++ test/testsuppressions.cpp | 13 ++++++++++--- test/testthreadexecutor.cpp | 3 +++ 7 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index ead92a228974..c25f2e4f8078 100644 --- a/Makefile +++ b/Makefile @@ -650,7 +650,7 @@ $(libcppdir)/vfvalue.o: lib/vfvalue.cpp lib/config.h lib/errortypes.h lib/mathli cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/filelister.h externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h lib/xml.h $(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cmdlineparser.cpp -cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h cli/cppcheckexecutorsig.h cli/executor.h cli/processexecutor.h cli/singleexecutor.h cli/threadexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkersreport.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h +cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h cli/cppcheckexecutorsig.h cli/executor.h cli/processexecutor.h cli/singleexecutor.h cli/threadexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkersreport.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h $(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutor.cpp cli/cppcheckexecutorseh.o: cli/cppcheckexecutorseh.cpp cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h lib/config.h lib/filesettings.h lib/platform.h lib/standards.h lib/utils.h @@ -806,7 +806,7 @@ test/testpostfixoperator.o: test/testpostfixoperator.cpp lib/addoninfo.h lib/che test/testpreprocessor.o: test/testpreprocessor.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testpreprocessor.cpp -test/testprocessexecutor.o: test/testprocessexecutor.cpp cli/executor.h cli/processexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h +test/testprocessexecutor.o: test/testprocessexecutor.cpp cli/executor.h cli/processexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testprocessexecutor.cpp test/testsettings.o: test/testsettings.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h @@ -824,7 +824,7 @@ test/testsimplifytypedef.o: test/testsimplifytypedef.cpp lib/addoninfo.h lib/che test/testsimplifyusing.o: test/testsimplifyusing.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.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 $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsimplifyusing.cpp -test/testsingleexecutor.o: test/testsingleexecutor.cpp cli/executor.h cli/singleexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h +test/testsingleexecutor.o: test/testsingleexecutor.cpp cli/executor.h cli/singleexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsingleexecutor.cpp test/testsizeof.o: test/testsizeof.cpp lib/addoninfo.h lib/check.h lib/checksizeof.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h @@ -839,13 +839,13 @@ test/teststring.o: test/teststring.cpp lib/addoninfo.h lib/check.h lib/checkstri test/testsummaries.o: test/testsummaries.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsummaries.cpp -test/testsuppressions.o: test/testsuppressions.cpp cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/singleexecutor.h cli/threadexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h +test/testsuppressions.o: test/testsuppressions.cpp cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/singleexecutor.h cli/threadexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsuppressions.cpp test/testsymboldatabase.o: test/testsymboldatabase.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.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 $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsymboldatabase.cpp -test/testthreadexecutor.o: test/testthreadexecutor.cpp cli/executor.h cli/threadexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h +test/testthreadexecutor.o: test/testthreadexecutor.cpp cli/executor.h cli/threadexecutor.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testthreadexecutor.cpp test/testtimer.o: test/testtimer.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h test/fixture.h diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 40fa87842a0a..d2e3d90944de 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -33,8 +33,6 @@ #include "suppressions.h" #include "utils.h" -#include "checkunusedfunctions.h" - #if defined(THREADING_MODEL_THREAD) #include "threadexecutor.h" #elif defined(THREADING_MODEL_FORK) @@ -178,8 +176,6 @@ class CppCheckExecutor::StdLogger : public ErrorLogger int CppCheckExecutor::check(int argc, const char* const argv[]) { - CheckUnusedFunctions::clear(); - Settings settings; CmdLineLoggerStd logger; CmdLineParser parser(logger, settings, settings.nomsg, settings.nofail); diff --git a/lib/checkunusedfunctions.h b/lib/checkunusedfunctions.h index b628aebef051..c92f7d4f078e 100644 --- a/lib/checkunusedfunctions.h +++ b/lib/checkunusedfunctions.h @@ -39,12 +39,14 @@ class Tokenizer; class CPPCHECKLIB CheckUnusedFunctions { friend class TestUnusedFunctions; + friend class TestSuppressions; + friend class TestSingleExecutorBase; + friend class TestProcessExecutorBase; + friend class TestThreadExecutorBase; public: CheckUnusedFunctions() = default; - static void clear(); - // Parse current tokens and determine.. // * Check what functions are used // * What functions are declared @@ -63,6 +65,8 @@ class CPPCHECKLIB CheckUnusedFunctions { static bool check(const Settings& settings, ErrorLogger &errorLogger); private: + static void clear(); + // Return true if an error is reported. bool check(ErrorLogger& errorLogger, const Settings& settings) const; diff --git a/test/testprocessexecutor.cpp b/test/testprocessexecutor.cpp index 4cb68c4ae422..654e1e5b8a0e 100644 --- a/test/testprocessexecutor.cpp +++ b/test/testprocessexecutor.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include "checkunusedfunctions.h" #include "processexecutor.h" #include "redirect.h" #include "settings.h" @@ -68,6 +69,8 @@ class TestProcessExecutorBase : public TestFixture { void check(unsigned int jobs, int files, int result, const std::string &data, const CheckOptions& opt = make_default_obj{}) { errout.str(""); + CheckUnusedFunctions::clear(); + std::list fileSettings; std::list> filelist; diff --git a/test/testsingleexecutor.cpp b/test/testsingleexecutor.cpp index 6abdcf23933f..4a2b9148e869 100644 --- a/test/testsingleexecutor.cpp +++ b/test/testsingleexecutor.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include "checkunusedfunctions.h" #include "cppcheck.h" #include "filesettings.h" #include "fixture.h" @@ -73,6 +74,8 @@ class TestSingleExecutorBase : public TestFixture { void check(int files, int result, const std::string &data, const CheckOptions& opt = make_default_obj{}) { errout.str(""); + CheckUnusedFunctions::clear(); + std::list fileSettings; std::list> filelist; diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index b65616177fb9..0f08e850ba27 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include "checkunusedfunctions.h" #include "config.h" #include "cppcheck.h" #include "cppcheckexecutor.h" @@ -229,6 +230,8 @@ class TestSuppressions : public TestFixture { // Clear the error log errout.str(""); + CheckUnusedFunctions::clear(); + std::list fileSettings; std::list> filelist; @@ -281,6 +284,8 @@ class TestSuppressions : public TestFixture { unsigned int _checkSuppressionThreads(const char code[], bool useFS, const std::string &suppression = emptyString) { errout.str(""); + CheckUnusedFunctions::clear(); + std::list fileSettings; std::list> filelist; @@ -329,6 +334,8 @@ class TestSuppressions : public TestFixture { unsigned int _checkSuppressionProcesses(const char code[], bool useFS, const std::string &suppression = emptyString) { errout.str(""); + CheckUnusedFunctions::clear(); + std::list fileSettings; std::list> filelist; @@ -1352,7 +1359,7 @@ class TestSuppressions : public TestFixture { // multi error in file, but only suppression one error const char code2[] = "fi fi\n" "if if;"; - ASSERT_EQUALS(2, (this->*check)(code2, "*:test.cpp:1")); // suppress all error at line 1 of test.cpp + ASSERT_EQUALS(1, (this->*check)(code2, "*:test.cpp:1")); // suppress all error at line 1 of test.cpp ASSERT_EQUALS("[test.cpp:2]: (error) syntax error\n", errout.str()); // multi error in file, but only suppression one error (2) @@ -1361,7 +1368,7 @@ class TestSuppressions : public TestFixture { " int b = y/0;\n" "}\n" "f(0, 1);\n"; - ASSERT_EQUALS(2, (this->*check)(code3, "zerodiv:test.cpp:3")); // suppress 'errordiv' at line 3 of test.cpp + ASSERT_EQUALS(1, (this->*check)(code3, "zerodiv:test.cpp:3")); // suppress 'errordiv' at line 3 of test.cpp } void suppressingSyntaxErrorAndExitCodeFiles() { @@ -1378,7 +1385,7 @@ class TestSuppressions : public TestFixture { std::map mfiles; mfiles["test.cpp"] = "fi if;"; mfiles["test2.cpp"] = "fi if"; - ASSERT_EQUALS(2, (this->*check)(mfiles, "*:test.cpp")); + ASSERT_EQUALS(1, (this->*check)(mfiles, "*:test.cpp")); ASSERT_EQUALS("[test2.cpp:1]: (error) syntax error\n", errout.str()); } diff --git a/test/testthreadexecutor.cpp b/test/testthreadexecutor.cpp index 229d0dc3d6a8..7a8bfa3e7e89 100644 --- a/test/testthreadexecutor.cpp +++ b/test/testthreadexecutor.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include "checkunusedfunctions.h" #include "redirect.h" #include "settings.h" #include "filesettings.h" @@ -68,6 +69,8 @@ class TestThreadExecutorBase : public TestFixture { void check(unsigned int jobs, int files, int result, const std::string &data, const CheckOptions& opt = make_default_obj{}) { errout.str(""); + CheckUnusedFunctions::clear(); + std::list fileSettings; std::list> filelist;