2009-02-19 23:21:18 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2021-03-21 20:58:32 +01:00
|
|
|
* Copyright (C) 2007-2021 Cppcheck team.
|
2009-02-19 23:21:18 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-02-19 23:21:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "threadexecutor.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2021-07-08 21:21:35 +02:00
|
|
|
#include "color.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
2009-02-19 23:21:18 +01:00
|
|
|
#include "cppcheck.h"
|
2013-09-04 06:18:22 +02:00
|
|
|
#include "cppcheckexecutor.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "errortypes.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "importproject.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "suppressions.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2017-09-07 13:02:44 +02:00
|
|
|
#include <cerrno>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <functional>
|
2014-05-23 14:17:39 +02:00
|
|
|
#include <iostream>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <utility>
|
|
|
|
|
Many platforms don't support backtraces. Fix compile for Solaris platform.
This change was tested with Solaris 10 on X86 and SPARC.
More information on Unix Backtrace Support
- http://www.gnu.org/software/libc/manual/html_node/Backtraces.html
It is not supported on the following platforms:
- https://www.gnu.org/software/gnulib/manual/html_node/execinfo_002eh.html
Mac OS X 10.3,
FreeBSD 6.0,
NetBSD 5.0,
OpenBSD 3.8,
Minix 3.1.8,
AIX 5.1,
HP-UX 11,
IRIX 6.5,
OSF/1 5.1,
Solaris 10,
Cygwin,
mingw,
MSVC 9,
Interix 3.5,
BeOS.
2014-06-25 14:45:18 +02:00
|
|
|
#ifdef __SVR4 // Solaris
|
|
|
|
#include <sys/loadavg.h>
|
|
|
|
#endif
|
2022-02-01 17:19:19 +01:00
|
|
|
|
2011-03-04 23:27:29 +01:00
|
|
|
#ifdef THREADING_MODEL_FORK
|
2020-02-18 10:09:29 +01:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
2013-01-13 13:21:46 +01:00
|
|
|
#include <sys/select.h>
|
2009-02-20 20:40:42 +01:00
|
|
|
#include <sys/wait.h>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <fcntl.h>
|
2022-02-01 17:19:19 +01:00
|
|
|
#include <csignal>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <unistd.h>
|
2009-02-20 20:40:42 +01:00
|
|
|
#endif
|
2022-02-01 17:19:19 +01:00
|
|
|
|
2012-12-12 19:09:37 +01:00
|
|
|
#ifdef THREADING_MODEL_WIN
|
2021-08-26 19:36:31 +02:00
|
|
|
#include <future>
|
|
|
|
#include <numeric>
|
2012-12-12 19:09:37 +01:00
|
|
|
#endif
|
2009-02-19 23:21:18 +01:00
|
|
|
|
2012-12-27 11:51:12 +01:00
|
|
|
// required for FD_ZERO
|
|
|
|
using std::memset;
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
ThreadExecutor::ThreadExecutor(const std::map<std::string, std::size_t> &files, Settings &settings, ErrorLogger &errorLogger)
|
2019-07-15 18:45:06 +02:00
|
|
|
: mFiles(files), mSettings(settings), mErrorLogger(errorLogger), mFileCount(0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2012-12-13 21:03:41 +01:00
|
|
|
#if defined(THREADING_MODEL_FORK)
|
2019-07-15 18:45:06 +02:00
|
|
|
mWpipe = 0;
|
2012-12-13 21:03:41 +01:00
|
|
|
#elif defined(THREADING_MODEL_WIN)
|
2019-07-15 18:45:06 +02:00
|
|
|
mProcessedFiles = 0;
|
|
|
|
mTotalFiles = 0;
|
|
|
|
mProcessedSize = 0;
|
|
|
|
mTotalFileSize = 0;
|
2010-12-09 19:03:41 +01:00
|
|
|
#endif
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadExecutor::~ThreadExecutor()
|
2022-01-18 22:02:25 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
// cppcheck-suppress unusedFunction - only used in unit tests
|
|
|
|
void ThreadExecutor::addFileContent(const std::string &path, const std::string &content)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2022-01-18 22:02:25 +01:00
|
|
|
mFileContents[path] = content;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
void ThreadExecutor::reportErr(const ErrorMessage &msg)
|
|
|
|
{
|
|
|
|
report(msg, MessageType::REPORT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportInfo(const ErrorMessage &msg)
|
|
|
|
{
|
|
|
|
report(msg, MessageType::REPORT_INFO);
|
|
|
|
}
|
2010-06-14 22:18:09 +02:00
|
|
|
|
2009-02-20 20:40:42 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-03-04 23:27:29 +01:00
|
|
|
////// This code is for platforms that support fork() only ////////////////////
|
2009-02-20 20:40:42 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-12-12 19:09:37 +01:00
|
|
|
#if defined(THREADING_MODEL_FORK)
|
2009-02-20 20:40:42 +01:00
|
|
|
|
2011-03-05 04:43:22 +01:00
|
|
|
int ThreadExecutor::handleRead(int rpipe, unsigned int &result)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
char type = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (read(rpipe, &type, 1) <= 0) {
|
2010-07-31 14:44:08 +02:00
|
|
|
if (errno == EAGAIN)
|
2010-07-07 14:42:39 +02:00
|
|
|
return 0;
|
|
|
|
|
2021-01-13 12:41:59 +01:00
|
|
|
// need to increment so a missing pipe (i.e. premature exit of forked process) results in an error exitcode
|
|
|
|
++result;
|
2010-07-07 14:42:39 +02:00
|
|
|
return -1;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2012-10-19 19:26:42 +02:00
|
|
|
if (type != REPORT_OUT && type != REPORT_ERROR && type != REPORT_INFO && type != CHILD_END) {
|
2019-03-29 06:53:40 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::handleRead error, type was:" << type << std::endl;
|
2021-01-13 12:41:59 +01:00
|
|
|
std::exit(EXIT_FAILURE);
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int len = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (read(rpipe, &len, sizeof(len)) <= 0) {
|
2019-03-29 06:53:40 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::handleRead error, type was:" << type << std::endl;
|
2021-01-13 12:41:59 +01:00
|
|
|
std::exit(EXIT_FAILURE);
|
2009-03-01 21:08:47 +01:00
|
|
|
}
|
|
|
|
|
2017-08-21 17:25:12 +02:00
|
|
|
// Don't rely on incoming data being null-terminated.
|
|
|
|
// Allocate +1 element and null-terminate the buffer.
|
|
|
|
char *buf = new char[len + 1];
|
|
|
|
const ssize_t readIntoBuf = read(rpipe, buf, len);
|
|
|
|
if (readIntoBuf <= 0) {
|
2019-03-29 06:53:40 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::handleRead error, type was:" << type << std::endl;
|
2021-01-13 12:41:59 +01:00
|
|
|
std::exit(EXIT_FAILURE);
|
2009-03-01 21:08:47 +01:00
|
|
|
}
|
2017-08-21 17:25:12 +02:00
|
|
|
buf[readIntoBuf] = 0;
|
2009-03-01 21:08:47 +01:00
|
|
|
|
2012-01-03 19:51:16 +01:00
|
|
|
if (type == REPORT_OUT) {
|
2019-07-15 18:45:06 +02:00
|
|
|
mErrorLogger.reportOut(buf);
|
2012-10-19 19:26:42 +02:00
|
|
|
} else if (type == REPORT_ERROR || type == REPORT_INFO) {
|
2020-05-23 07:16:49 +02:00
|
|
|
ErrorMessage msg;
|
2021-10-20 20:41:42 +02:00
|
|
|
try {
|
|
|
|
msg.deserialize(buf);
|
|
|
|
} catch (const InternalError& e) {
|
|
|
|
std::cerr << "#### ThreadExecutor::handleRead error, internal error:" << e.errorMessage << std::endl;
|
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
2009-03-01 20:34:32 +01:00
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
if (!mSettings.nomsg.isSuppressed(msg.toSuppressionsErrorMessage())) {
|
2011-02-16 02:12:15 +01:00
|
|
|
// Alert only about unique errors
|
2019-07-15 18:45:06 +02:00
|
|
|
std::string errmsg = msg.toString(mSettings.verbose);
|
|
|
|
if (std::find(mErrorList.begin(), mErrorList.end(), errmsg) == mErrorList.end()) {
|
2020-05-11 13:48:54 +02:00
|
|
|
mErrorList.emplace_back(errmsg);
|
2012-10-19 20:17:34 +02:00
|
|
|
if (type == REPORT_ERROR)
|
2019-07-15 18:45:06 +02:00
|
|
|
mErrorLogger.reportErr(msg);
|
2012-10-19 20:17:34 +02:00
|
|
|
else
|
2019-07-15 18:45:06 +02:00
|
|
|
mErrorLogger.reportInfo(msg);
|
2011-02-16 02:12:15 +01:00
|
|
|
}
|
2009-03-01 20:34:32 +01:00
|
|
|
}
|
2012-01-03 19:51:16 +01:00
|
|
|
} else if (type == CHILD_END) {
|
2009-02-19 23:21:18 +01:00
|
|
|
std::istringstream iss(buf);
|
|
|
|
unsigned int fileResult = 0;
|
|
|
|
iss >> fileResult;
|
|
|
|
result += fileResult;
|
2021-08-07 20:51:18 +02:00
|
|
|
delete[] buf;
|
2010-07-07 14:42:39 +02:00
|
|
|
return -1;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
delete[] buf;
|
2010-07-07 14:42:39 +02:00
|
|
|
return 1;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2014-10-01 19:33:02 +02:00
|
|
|
bool ThreadExecutor::checkLoadAverage(size_t nchildren)
|
2014-03-25 18:35:59 +01:00
|
|
|
{
|
2021-07-20 11:04:20 +02:00
|
|
|
#if defined(__CYGWIN__) || defined(__QNX__) || defined(__HAIKU__) // getloadavg() is unsupported on Cygwin, Qnx, Haiku.
|
2014-04-05 20:47:02 +02:00
|
|
|
return true;
|
|
|
|
#else
|
2019-07-15 18:45:06 +02:00
|
|
|
if (!nchildren || !mSettings.loadAverage) {
|
2014-03-25 18:35:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-05 20:47:02 +02:00
|
|
|
double sample(0);
|
2014-03-25 18:35:59 +01:00
|
|
|
if (getloadavg(&sample, 1) != 1) {
|
2014-04-05 20:47:02 +02:00
|
|
|
// disable load average checking on getloadavg error
|
2014-03-25 18:35:59 +01:00
|
|
|
return true;
|
2019-07-15 18:45:06 +02:00
|
|
|
} else if (sample < mSettings.loadAverage) {
|
2014-03-25 18:35:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-04-05 20:47:02 +02:00
|
|
|
#endif
|
2014-03-25 18:35:59 +01:00
|
|
|
}
|
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
unsigned int ThreadExecutor::check()
|
|
|
|
{
|
2019-07-15 18:45:06 +02:00
|
|
|
mFileCount = 0;
|
2009-02-19 23:21:18 +01:00
|
|
|
unsigned int result = 0;
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t totalfilesize = 0;
|
2019-07-15 18:45:06 +02:00
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i) {
|
2011-04-19 13:52:32 +02:00
|
|
|
totalfilesize += i->second;
|
|
|
|
}
|
|
|
|
|
2011-03-05 04:43:22 +01:00
|
|
|
std::list<int> rpipes;
|
|
|
|
std::map<pid_t, std::string> childFile;
|
2011-04-19 13:52:32 +02:00
|
|
|
std::map<int, std::string> pipeFile;
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t processedsize = 0;
|
2019-07-15 18:45:06 +02:00
|
|
|
std::map<std::string, std::size_t>::const_iterator iFile = mFiles.begin();
|
|
|
|
std::list<ImportProject::FileSettings>::const_iterator iFileSettings = mSettings.project.fileSettings.begin();
|
2012-03-24 21:47:52 +01:00
|
|
|
for (;;) {
|
2010-07-07 14:42:39 +02:00
|
|
|
// Start a new child
|
2020-12-03 18:06:18 +01:00
|
|
|
size_t nchildren = childFile.size();
|
2019-07-15 18:45:06 +02:00
|
|
|
if ((iFile != mFiles.end() || iFileSettings != mSettings.project.fileSettings.end()) && nchildren < mSettings.jobs && checkLoadAverage(nchildren)) {
|
2011-03-05 04:43:22 +01:00
|
|
|
int pipes[2];
|
2011-10-13 20:53:06 +02:00
|
|
|
if (pipe(pipes) == -1) {
|
2019-03-29 06:53:40 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::check, pipe() failed: "<< std::strerror(errno) << std::endl;
|
2012-12-27 11:51:12 +01:00
|
|
|
std::exit(EXIT_FAILURE);
|
2011-03-05 04:43:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int flags = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
if ((flags = fcntl(pipes[0], F_GETFL, 0)) < 0) {
|
2019-03-29 06:53:40 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::check, fcntl(F_GETFL) failed: "<< std::strerror(errno) << std::endl;
|
2012-12-27 11:51:12 +01:00
|
|
|
std::exit(EXIT_FAILURE);
|
2011-03-05 04:43:22 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (fcntl(pipes[0], F_SETFL, flags | O_NONBLOCK) < 0) {
|
2019-03-29 06:53:40 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::check, fcntl(F_SETFL) failed: "<< std::strerror(errno) << std::endl;
|
2012-12-27 11:51:12 +01:00
|
|
|
std::exit(EXIT_FAILURE);
|
2011-03-05 04:43:22 +01:00
|
|
|
}
|
|
|
|
|
2010-07-07 14:42:39 +02:00
|
|
|
pid_t pid = fork();
|
2011-10-13 20:53:06 +02:00
|
|
|
if (pid < 0) {
|
2010-07-07 14:42:39 +02:00
|
|
|
// Error
|
2019-03-29 06:53:40 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::check, Failed to create child process: "<< std::strerror(errno) << std::endl;
|
2012-12-27 11:51:12 +01:00
|
|
|
std::exit(EXIT_FAILURE);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (pid == 0) {
|
2020-02-18 10:09:29 +01:00
|
|
|
#if defined(__linux__)
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
|
|
|
#endif
|
2011-03-05 04:43:22 +01:00
|
|
|
close(pipes[0]);
|
2019-07-15 18:45:06 +02:00
|
|
|
mWpipe = pipes[1];
|
2011-03-05 04:43:22 +01:00
|
|
|
|
2020-05-19 16:04:25 +02:00
|
|
|
CppCheck fileChecker(*this, false, CppCheckExecutor::executeCommand);
|
2019-07-15 18:45:06 +02:00
|
|
|
fileChecker.settings() = mSettings;
|
2011-04-24 18:10:25 +02:00
|
|
|
unsigned int resultOfCheck = 0;
|
2010-07-07 14:42:39 +02:00
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
if (iFileSettings != mSettings.project.fileSettings.end()) {
|
2016-08-13 10:50:03 +02:00
|
|
|
resultOfCheck = fileChecker.check(*iFileSettings);
|
2019-07-15 18:45:06 +02:00
|
|
|
} else if (!mFileContents.empty() && mFileContents.find(iFile->first) != mFileContents.end()) {
|
2010-07-07 14:42:39 +02:00
|
|
|
// File content was given as a string
|
2021-08-07 20:51:18 +02:00
|
|
|
resultOfCheck = fileChecker.check(iFile->first, mFileContents[iFile->first]);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-07-07 14:42:39 +02:00
|
|
|
// Read file from a file
|
2016-08-13 10:50:03 +02:00
|
|
|
resultOfCheck = fileChecker.check(iFile->first);
|
2010-07-07 14:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << resultOfCheck;
|
2012-01-03 19:51:16 +01:00
|
|
|
writeToPipe(CHILD_END, oss.str());
|
2021-01-13 12:41:59 +01:00
|
|
|
std::exit(EXIT_SUCCESS);
|
2010-07-07 14:42:39 +02:00
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
|
2011-03-05 04:43:22 +01:00
|
|
|
close(pipes[1]);
|
|
|
|
rpipes.push_back(pipes[0]);
|
2019-07-15 18:45:06 +02:00
|
|
|
if (iFileSettings != mSettings.project.fileSettings.end()) {
|
2016-08-13 10:50:03 +02:00
|
|
|
childFile[pid] = iFileSettings->filename + ' ' + iFileSettings->cfg;
|
|
|
|
pipeFile[pipes[0]] = iFileSettings->filename + ' ' + iFileSettings->cfg;
|
|
|
|
++iFileSettings;
|
|
|
|
} else {
|
|
|
|
childFile[pid] = iFile->first;
|
|
|
|
pipeFile[pipes[0]] = iFile->first;
|
|
|
|
++iFile;
|
|
|
|
}
|
2020-12-04 08:07:14 +01:00
|
|
|
}
|
|
|
|
if (!rpipes.empty()) {
|
2011-03-05 04:43:22 +01:00
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
for (std::list<int>::const_iterator rp = rpipes.begin(); rp != rpipes.end(); ++rp)
|
|
|
|
FD_SET(*rp, &rfds);
|
2014-03-25 18:35:59 +01:00
|
|
|
struct timeval tv; // for every second polling of load average condition
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
2015-11-30 22:13:49 +01:00
|
|
|
int r = select(*std::max_element(rpipes.begin(), rpipes.end()) + 1, &rfds, nullptr, nullptr, &tv);
|
2011-03-05 04:43:22 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (r > 0) {
|
2011-03-05 04:43:22 +01:00
|
|
|
std::list<int>::iterator rp = rpipes.begin();
|
2011-10-13 20:53:06 +02:00
|
|
|
while (rp != rpipes.end()) {
|
|
|
|
if (FD_ISSET(*rp, &rfds)) {
|
2011-03-05 04:43:22 +01:00
|
|
|
int readRes = handleRead(*rp, result);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (readRes == -1) {
|
2013-02-10 07:43:09 +01:00
|
|
|
std::size_t size = 0;
|
2011-04-19 13:52:32 +02:00
|
|
|
std::map<int, std::string>::iterator p = pipeFile.find(*rp);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (p != pipeFile.end()) {
|
2011-04-19 13:52:32 +02:00
|
|
|
std::string name = p->second;
|
|
|
|
pipeFile.erase(p);
|
2019-07-15 18:45:06 +02:00
|
|
|
std::map<std::string, std::size_t>::const_iterator fs = mFiles.find(name);
|
|
|
|
if (fs != mFiles.end()) {
|
2011-04-19 13:52:32 +02:00
|
|
|
size = fs->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
mFileCount++;
|
2011-04-19 13:52:32 +02:00
|
|
|
processedsize += size;
|
2019-07-15 18:45:06 +02:00
|
|
|
if (!mSettings.quiet)
|
|
|
|
CppCheckExecutor::reportStatus(mFileCount, mFiles.size() + mSettings.project.fileSettings.size(), processedsize, totalfilesize);
|
2011-04-19 13:52:32 +02:00
|
|
|
|
2011-03-05 04:43:22 +01:00
|
|
|
close(*rp);
|
|
|
|
rp = rpipes.erase(rp);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2011-03-05 04:43:22 +01:00
|
|
|
++rp;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2011-03-05 04:43:22 +01:00
|
|
|
++rp;
|
2010-12-30 21:35:53 +01:00
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
2020-12-04 08:07:14 +01:00
|
|
|
}
|
|
|
|
if (!childFile.empty()) {
|
2009-02-19 23:21:18 +01:00
|
|
|
int stat = 0;
|
2011-03-05 04:43:22 +01:00
|
|
|
pid_t child = waitpid(0, &stat, WNOHANG);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (child > 0) {
|
2011-03-05 04:43:22 +01:00
|
|
|
std::string childname;
|
|
|
|
std::map<pid_t, std::string>::iterator c = childFile.find(child);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (c != childFile.end()) {
|
2011-03-05 04:43:22 +01:00
|
|
|
childname = c->second;
|
|
|
|
childFile.erase(c);
|
|
|
|
}
|
|
|
|
|
2020-12-03 18:06:18 +01:00
|
|
|
if (WIFEXITED(stat)) {
|
2021-01-13 12:41:59 +01:00
|
|
|
const int exitstatus = WEXITSTATUS(stat);
|
|
|
|
if (exitstatus != EXIT_SUCCESS) {
|
2020-12-03 18:06:18 +01:00
|
|
|
std::ostringstream oss;
|
2021-01-13 12:41:59 +01:00
|
|
|
oss << "Child process exited with " << exitstatus;
|
2020-12-03 18:06:18 +01:00
|
|
|
reportInternalChildErr(childname, oss.str());
|
|
|
|
}
|
|
|
|
} else if (WIFSIGNALED(stat)) {
|
2011-03-05 04:43:22 +01:00
|
|
|
std::ostringstream oss;
|
2020-12-03 18:06:18 +01:00
|
|
|
oss << "Child process crashed with signal " << WTERMSIG(stat);
|
|
|
|
reportInternalChildErr(childname, oss.str());
|
2011-03-05 04:43:22 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 08:07:14 +01:00
|
|
|
}
|
|
|
|
if (iFile == mFiles.end() && iFileSettings == mSettings.project.fileSettings.end() && rpipes.empty() && childFile.empty()) {
|
2010-07-07 14:42:39 +02:00
|
|
|
// All done
|
|
|
|
break;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-03 21:21:17 +01:00
|
|
|
void ThreadExecutor::writeToPipe(PipeSignal type, const std::string &data)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2012-01-03 21:21:17 +01:00
|
|
|
unsigned int len = static_cast<unsigned int>(data.length() + 1);
|
2021-08-07 20:51:18 +02:00
|
|
|
char *out = new char[len + 1 + sizeof(len)];
|
2012-01-03 21:21:17 +01:00
|
|
|
out[0] = static_cast<char>(type);
|
2009-02-20 20:53:14 +01:00
|
|
|
std::memcpy(&(out[1]), &len, sizeof(len));
|
|
|
|
std::memcpy(&(out[1+sizeof(len)]), data.c_str(), len);
|
2019-07-15 18:45:06 +02:00
|
|
|
if (write(mWpipe, out, len + 1 + sizeof(len)) <= 0) {
|
2021-08-07 20:51:18 +02:00
|
|
|
delete[] out;
|
2017-09-07 10:33:52 +02:00
|
|
|
out = nullptr;
|
2009-03-01 21:08:47 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::writeToPipe, Failed to write to pipe" << std::endl;
|
2021-01-13 12:41:59 +01:00
|
|
|
std::exit(EXIT_FAILURE);
|
2009-03-01 21:08:47 +01:00
|
|
|
}
|
2009-05-09 21:40:22 +02:00
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
delete[] out;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:21:35 +02:00
|
|
|
void ThreadExecutor::reportOut(const std::string &outmsg, Color c)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2021-07-08 21:21:35 +02:00
|
|
|
writeToPipe(REPORT_OUT, ::toString(c) + outmsg + ::toString(Color::Reset));
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
void ThreadExecutor::bughuntingReport(const std::string &str)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2022-01-18 22:02:25 +01:00
|
|
|
writeToPipe(REPORT_VERIFICATION, str);
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
void ThreadExecutor::report(const ErrorMessage &msg, MessageType msgType)
|
2012-06-18 23:15:48 +02:00
|
|
|
{
|
2022-01-18 22:02:25 +01:00
|
|
|
PipeSignal pipeSignal;
|
|
|
|
switch (msgType) {
|
|
|
|
case MessageType::REPORT_ERROR:
|
|
|
|
pipeSignal = REPORT_ERROR;
|
|
|
|
break;
|
|
|
|
case MessageType::REPORT_INFO:
|
|
|
|
pipeSignal = REPORT_INFO;
|
|
|
|
break;
|
|
|
|
}
|
2012-06-18 23:15:48 +02:00
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
writeToPipe(pipeSignal, msg.serialize());
|
2019-12-27 19:05:10 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 18:06:18 +01:00
|
|
|
void ThreadExecutor::reportInternalChildErr(const std::string &childname, const std::string &msg)
|
|
|
|
{
|
|
|
|
std::list<ErrorMessage::FileLocation> locations;
|
|
|
|
locations.emplace_back(childname, 0, 0);
|
|
|
|
const ErrorMessage errmsg(locations,
|
|
|
|
emptyString,
|
|
|
|
Severity::error,
|
|
|
|
"Internal error: " + msg,
|
|
|
|
"cppcheckError",
|
2021-02-24 22:12:48 +01:00
|
|
|
Certainty::normal);
|
2020-12-03 18:06:18 +01:00
|
|
|
|
|
|
|
if (!mSettings.nomsg.isSuppressed(errmsg.toSuppressionsErrorMessage()))
|
|
|
|
mErrorLogger.reportErr(errmsg);
|
|
|
|
}
|
|
|
|
|
2012-12-12 19:09:37 +01:00
|
|
|
#elif defined(THREADING_MODEL_WIN)
|
|
|
|
|
|
|
|
unsigned int ThreadExecutor::check()
|
|
|
|
{
|
2021-08-24 20:39:43 +02:00
|
|
|
std::vector<std::future<unsigned int>> threadFutures;
|
|
|
|
threadFutures.reserve(mSettings.jobs);
|
2012-12-12 19:09:37 +01:00
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
mItNextFile = mFiles.begin();
|
|
|
|
mItNextFileSettings = mSettings.project.fileSettings.begin();
|
2012-12-12 19:09:37 +01:00
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
mProcessedFiles = 0;
|
|
|
|
mProcessedSize = 0;
|
|
|
|
mTotalFiles = mFiles.size() + mSettings.project.fileSettings.size();
|
|
|
|
mTotalFileSize = 0;
|
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i) {
|
|
|
|
mTotalFileSize += i->second;
|
2012-12-13 18:47:13 +01:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
for (unsigned int i = 0; i < mSettings.jobs; ++i) {
|
2021-08-24 20:39:43 +02:00
|
|
|
try {
|
|
|
|
threadFutures.emplace_back(std::async(std::launch::async, threadProc, this));
|
2012-12-12 19:09:37 +01:00
|
|
|
}
|
2021-08-24 20:39:43 +02:00
|
|
|
catch (const std::system_error &e) {
|
|
|
|
std::cerr << "#### ThreadExecutor::check exception :" << e.what() << std::endl;
|
2012-12-12 19:09:37 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 20:39:43 +02:00
|
|
|
return std::accumulate(threadFutures.begin(), threadFutures.end(), 0U, [](unsigned int v, std::future<unsigned int>& f) {
|
|
|
|
return v + f.get();
|
|
|
|
});
|
2012-12-12 19:09:37 +01:00
|
|
|
}
|
|
|
|
|
2021-08-24 20:39:43 +02:00
|
|
|
unsigned int __stdcall ThreadExecutor::threadProc(ThreadExecutor* threadExecutor)
|
2012-12-12 19:09:37 +01:00
|
|
|
{
|
|
|
|
unsigned int result = 0;
|
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
std::map<std::string, std::size_t>::const_iterator &itFile = threadExecutor->mItNextFile;
|
|
|
|
std::list<ImportProject::FileSettings>::const_iterator &itFileSettings = threadExecutor->mItNextFileSettings;
|
2012-12-12 19:09:37 +01:00
|
|
|
|
|
|
|
// guard static members of CppCheck against concurrent access
|
2021-08-24 20:39:43 +02:00
|
|
|
threadExecutor->mFileSync.lock();
|
2012-12-12 19:09:37 +01:00
|
|
|
|
|
|
|
for (;;) {
|
2019-07-15 18:45:06 +02:00
|
|
|
if (itFile == threadExecutor->mFiles.end() && itFileSettings == threadExecutor->mSettings.project.fileSettings.end()) {
|
2021-08-24 20:39:43 +02:00
|
|
|
threadExecutor->mFileSync.unlock();
|
2015-01-21 18:46:33 +01:00
|
|
|
break;
|
2016-08-07 17:10:37 +02:00
|
|
|
}
|
2012-12-12 19:09:37 +01:00
|
|
|
|
2021-08-17 20:51:31 +02:00
|
|
|
CppCheck fileChecker(*threadExecutor, false, CppCheckExecutor::executeCommand);
|
|
|
|
fileChecker.settings() = threadExecutor->mSettings;
|
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
std::size_t fileSize = 0;
|
2019-07-15 18:45:06 +02:00
|
|
|
if (itFile != threadExecutor->mFiles.end()) {
|
2016-08-13 10:50:03 +02:00
|
|
|
const std::string &file = itFile->first;
|
|
|
|
fileSize = itFile->second;
|
|
|
|
++itFile;
|
2016-08-07 14:30:09 +02:00
|
|
|
|
2021-08-24 20:39:43 +02:00
|
|
|
threadExecutor->mFileSync.unlock();
|
2016-08-13 10:50:03 +02:00
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
const std::map<std::string, std::string>::const_iterator fileContent = threadExecutor->mFileContents.find(file);
|
|
|
|
if (fileContent != threadExecutor->mFileContents.end()) {
|
2016-08-13 10:50:03 +02:00
|
|
|
// File content was given as a string
|
|
|
|
result += fileChecker.check(file, fileContent->second);
|
|
|
|
} else {
|
|
|
|
// Read file from a file
|
|
|
|
result += fileChecker.check(file);
|
|
|
|
}
|
|
|
|
} else { // file settings..
|
|
|
|
const ImportProject::FileSettings &fs = *itFileSettings;
|
|
|
|
++itFileSettings;
|
2021-08-24 20:39:43 +02:00
|
|
|
threadExecutor->mFileSync.unlock();
|
2016-08-13 10:50:03 +02:00
|
|
|
result += fileChecker.check(fs);
|
2020-03-15 11:09:35 +01:00
|
|
|
if (threadExecutor->mSettings.clangTidy)
|
|
|
|
fileChecker.analyseClangTidy(fs);
|
2012-12-12 19:09:37 +01:00
|
|
|
}
|
|
|
|
|
2021-08-24 20:39:43 +02:00
|
|
|
threadExecutor->mFileSync.lock();
|
2012-12-13 18:47:13 +01:00
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
threadExecutor->mProcessedSize += fileSize;
|
|
|
|
threadExecutor->mProcessedFiles++;
|
|
|
|
if (!threadExecutor->mSettings.quiet) {
|
2021-08-24 20:39:43 +02:00
|
|
|
std::lock_guard<std::mutex> lg(threadExecutor->mReportSync);
|
2019-07-15 18:45:06 +02:00
|
|
|
CppCheckExecutor::reportStatus(threadExecutor->mProcessedFiles, threadExecutor->mTotalFiles, threadExecutor->mProcessedSize, threadExecutor->mTotalFileSize);
|
2012-12-13 18:47:13 +01:00
|
|
|
}
|
2015-01-21 18:46:33 +01:00
|
|
|
}
|
2012-12-12 19:09:37 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:21:35 +02:00
|
|
|
void ThreadExecutor::reportOut(const std::string &outmsg, Color c)
|
2012-12-12 19:09:37 +01:00
|
|
|
{
|
2021-08-24 20:39:43 +02:00
|
|
|
std::lock_guard<std::mutex> lg(mReportSync);
|
2012-12-12 19:09:37 +01:00
|
|
|
|
2021-07-08 21:21:35 +02:00
|
|
|
mErrorLogger.reportOut(outmsg, c);
|
2012-12-12 19:09:37 +01:00
|
|
|
}
|
2019-12-27 19:05:10 +01:00
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
void ThreadExecutor::bughuntingReport(const std::string & /*str*/)
|
2019-12-27 19:05:10 +01:00
|
|
|
{
|
|
|
|
// TODO
|
2012-12-12 19:09:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
void ThreadExecutor::report(const ErrorMessage &msg, MessageType msgType)
|
2012-12-12 19:09:37 +01:00
|
|
|
{
|
2019-07-15 18:45:06 +02:00
|
|
|
if (mSettings.nomsg.isSuppressed(msg.toSuppressionsErrorMessage()))
|
2012-12-12 19:09:37 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Alert only about unique errors
|
|
|
|
bool reportError = false;
|
2019-07-15 18:45:06 +02:00
|
|
|
const std::string errmsg = msg.toString(mSettings.verbose);
|
2012-12-12 19:09:37 +01:00
|
|
|
|
2021-08-24 20:39:43 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lg(mErrorSync);
|
|
|
|
if (std::find(mErrorList.begin(), mErrorList.end(), errmsg) == mErrorList.end()) {
|
|
|
|
mErrorList.emplace_back(errmsg);
|
|
|
|
reportError = true;
|
|
|
|
}
|
2012-12-12 19:09:37 +01:00
|
|
|
}
|
2021-08-24 20:39:43 +02:00
|
|
|
|
2012-12-12 19:09:37 +01:00
|
|
|
if (reportError) {
|
2021-08-24 20:39:43 +02:00
|
|
|
std::lock_guard<std::mutex> lg(mReportSync);
|
2012-12-12 19:09:37 +01:00
|
|
|
|
|
|
|
switch (msgType) {
|
2019-07-29 22:40:53 +02:00
|
|
|
case MessageType::REPORT_ERROR:
|
2019-07-15 18:45:06 +02:00
|
|
|
mErrorLogger.reportErr(msg);
|
2012-12-12 19:09:37 +01:00
|
|
|
break;
|
2019-07-29 22:40:53 +02:00
|
|
|
case MessageType::REPORT_INFO:
|
2019-07-15 18:45:06 +02:00
|
|
|
mErrorLogger.reportInfo(msg);
|
2012-12-12 19:09:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
#endif
|