added CMake option USE_THREADS to be able to use threads instead of fork() / cleanups (#3852)

This commit is contained in:
Oliver Stöneberg 2022-02-23 09:04:35 +01:00 committed by GitHub
parent b886b64b1a
commit 51371f7929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 56 additions and 24 deletions

View File

@ -581,7 +581,7 @@ $(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/utils.h
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/analyzer.h lib/astutils.h lib/calculate.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/forwardanalyzer.h lib/importproject.h lib/infer.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/programmemory.h lib/reverseanalyzer.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/valueflow.o $(libcppdir)/valueflow.cpp
cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlineparser.h cli/cppcheckexecutor.h cli/filelister.h cli/threadexecutor.h externals/tinyxml2/tinyxml2.h 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/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlineparser.h cli/cppcheckexecutor.h cli/filelister.h externals/tinyxml2/tinyxml2.h 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/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o cli/cmdlineparser.o cli/cmdlineparser.cpp
cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlineparser.h cli/cppcheckexecutor.h cli/filelister.h cli/threadexecutor.h externals/simplecpp/simplecpp.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/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h

View File

@ -37,6 +37,9 @@ endif()
if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2)
target_link_libraries(cppcheck ${tinyxml2_LIBRARY})
endif()
if (USE_THREADS)
target_link_libraries(cppcheck ${CMAKE_THREAD_LIBS_INIT})
endif()
add_dependencies(cppcheck copy_cfg)
add_dependencies(cppcheck copy_addons)

View File

@ -19,6 +19,7 @@
#include "cmdlineparser.h"
#include "check.h"
#include "config.h"
#include "cppcheckexecutor.h"
#include "errortypes.h"
#include "filelister.h"
@ -28,7 +29,6 @@
#include "settings.h"
#include "standards.h"
#include "suppressions.h"
#include "threadexecutor.h" // Threading model
#include "timer.h"
#include "utils.h"
@ -470,6 +470,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
}
}
#ifdef THREADING_MODEL_FORK
else if (std::strncmp(argv[i], "-l", 2) == 0) {
std::string numberString;
@ -494,6 +495,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
return false;
}
}
#endif
// Enforce language (--language=, -x)
else if (std::strncmp(argv[i], "--language=", 11) == 0 || std::strcmp(argv[i], "-x") == 0) {

View File

@ -19,19 +19,15 @@
#include "threadexecutor.h"
#include "color.h"
#include "config.h"
#include "cppcheck.h"
#include "cppcheckexecutor.h"
#include "errorlogger.h"
#include "errortypes.h"
#include "importproject.h"
#include "settings.h"
#include "suppressions.h"
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <utility>
@ -41,9 +37,14 @@
#endif
#ifdef THREADING_MODEL_FORK
#include "config.h"
#include "errortypes.h"
#if defined(__linux__)
#include <sys/prctl.h>
#endif
#include <cerrno>
#include <cstring>
#include <sys/select.h>
#include <sys/wait.h>
#include <fcntl.h>
@ -54,13 +55,13 @@
using std::memset;
#endif
#ifdef THREADING_MODEL_WIN
#ifdef THREADING_MODEL_THREAD
#include <future>
#include <numeric>
#endif
ThreadExecutor::ThreadExecutor(const std::map<std::string, std::size_t> &files, Settings &settings, ErrorLogger &errorLogger)
: mFiles(files), mSettings(settings), mErrorLogger(errorLogger), mFileCount(0)
: mFiles(files), mSettings(settings), mErrorLogger(errorLogger)
{}
ThreadExecutor::~ThreadExecutor()
@ -231,7 +232,7 @@ bool ThreadExecutor::checkLoadAverage(size_t nchildren)
unsigned int ThreadExecutor::check()
{
mFileCount = 0;
unsigned int fileCount = 0;
unsigned int result = 0;
std::size_t totalfilesize = 0;
@ -337,10 +338,10 @@ unsigned int ThreadExecutor::check()
}
}
mFileCount++;
fileCount++;
processedsize += size;
if (!mSettings.quiet)
CppCheckExecutor::reportStatus(mFileCount, mFiles.size() + mSettings.project.fileSettings.size(), processedsize, totalfilesize);
CppCheckExecutor::reportStatus(fileCount, mFiles.size() + mSettings.project.fileSettings.size(), processedsize, totalfilesize);
close(*rp);
rp = rpipes.erase(rp);
@ -401,7 +402,7 @@ void ThreadExecutor::reportInternalChildErr(const std::string &childname, const
mErrorLogger.reportErr(errmsg);
}
#elif defined(THREADING_MODEL_WIN)
#elif defined(THREADING_MODEL_THREAD)
class ThreadExecutor::LogWriter : public ErrorLogger
{
@ -510,7 +511,7 @@ unsigned int ThreadExecutor::check()
});
}
unsigned int __stdcall ThreadExecutor::threadProc(LogWriter* logWriter)
unsigned int STDCALL ThreadExecutor::threadProc(LogWriter* logWriter)
{
unsigned int result = 0;

View File

@ -19,19 +19,13 @@
#ifndef THREADEXECUTOR_H
#define THREADEXECUTOR_H
#include "config.h"
#include <cstddef>
#include <list>
#include <map>
#include <string>
#if ((defined(__GNUC__) || defined(__sun)) && !defined(__MINGW32__) && !defined(__CYGWIN__)) || defined(__CPPCHECK__)
#define THREADING_MODEL_FORK
#elif defined(_WIN32)
#define THREADING_MODEL_WIN
#else
#error "No threading model defined"
#endif
class Settings;
class ErrorLogger;
@ -63,7 +57,6 @@ private:
const std::map<std::string, std::size_t> &mFiles;
Settings &mSettings;
ErrorLogger &mErrorLogger;
unsigned int mFileCount;
std::list<std::string> mErrorList;
/** @brief Key is file name, and value is the content of the file */
@ -92,10 +85,10 @@ private:
*/
void reportInternalChildErr(const std::string &childname, const std::string &msg);
#elif defined(THREADING_MODEL_WIN)
#elif defined(THREADING_MODEL_THREAD)
class LogWriter;
static unsigned __stdcall threadProc(LogWriter *threadExecutor);
static unsigned int STDCALL threadProc(LogWriter *threadExecutor);
#endif

View File

@ -27,5 +27,9 @@ if (ENABLE_CHECK_INTERNAL)
add_definitions(-DCHECK_INTERNAL)
endif()
if (USE_THREADS)
add_definitions(-DUSE_THREADS)
endif()
file(TO_CMAKE_PATH ${FILESDIR} _filesdir)
add_definitions(-DFILESDIR="${_filesdir}")

View File

@ -56,3 +56,7 @@ if (NOT USE_BUNDLED_TINYXML2)
endif()
endif()
endif()
if (USE_THREADS)
find_package(Threads REQUIRED)
endif()

View File

@ -43,6 +43,7 @@ option(HAVE_RULES "Usage of rules (needs PCRE library and headers)"
option(USE_Z3 "Usage of z3 library" OFF)
option(USE_BUNDLED_TINYXML2 "Usage of bundled tinyxml2 library" ON)
option(CPPCHK_GLIBCXX_DEBUG "Usage of _GLIBCXX_DEBUG in Debug build" ON)
option(USE_THREADS "Usage of threads instead of fork() for -j" OFF)
if (CMAKE_VERSION VERSION_EQUAL "3.16" OR CMAKE_VERSION VERSION_GREATER "3.16")
set(CMAKE_DISABLE_PRECOMPILE_HEADERS Off CACHE BOOL "Disable precompiled headers")

View File

@ -47,7 +47,12 @@ if (HAVE_RULES)
message( STATUS "PCRE_LIBRARY = ${PCRE_LIBRARY}" )
endif()
message( STATUS )
message( STATUS "USE_THREADS = ${USE_THREADS}" )
if (USE_THREADS)
message( STATUS "CMAKE_THREAD_LIBS_INIT = ${CMAKE_THREAD_LIBS_INIT}" )
endif()
if (NOT USE_MATCHCOMPILER_OPT MATCHES "Off")
message( STATUS )
message( STATUS "PYTHON_VERSION_STRING = ${PYTHON_VERSION_STRING}" )
message( STATUS "PYTHON_EXECUTABLE = ${PYTHON_EXECUTABLE}" )
endif()

View File

@ -95,4 +95,16 @@ static const std::string emptyString;
#endif
#endif
#if defined(_WIN32)
#define THREADING_MODEL_THREAD
#define STDCALL __stdcall
#elif defined(USE_THREADS)
#define THREADING_MODEL_THREAD
#define STDCALL
#elif ((defined(__GNUC__) || defined(__sun)) && !defined(__MINGW32__) && !defined(__CYGWIN__)) || defined(__CPPCHECK__)
#define THREADING_MODEL_FORK
#else
#error "No threading model defined"
#endif
#endif // configH

View File

@ -59,7 +59,9 @@ Settings::Settings()
inlineSuppressions(false),
jobs(1),
jointSuppressionReport(false),
#ifdef THREADING_MODEL_FORK
loadAverage(0),
#endif
maxConfigs(12),
maxCtuDepth(2),
maxTemplateRecursion(100),

View File

@ -228,8 +228,10 @@ public:
/** Library */
Library library;
#ifdef THREADING_MODEL_FORK
/** @brief Load average value */
int loadAverage;
#endif
/** @brief Maximum number of configurations to check before bailing.
Default is 12. (--max-configs=N) */

View File

@ -28,6 +28,9 @@ if (BUILD_TESTS)
if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2)
target_link_libraries(testrunner ${tinyxml2_LIBRARY})
endif()
if (USE_THREADS)
target_link_libraries(testrunner ${CMAKE_THREAD_LIBS_INIT})
endif()
if (NOT CMAKE_DISABLE_PRECOMPILE_HEADERS)
target_precompile_headers(testrunner PRIVATE precompiled.h)