2009-02-19 23:21:18 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2014-02-15 07:45:39 +01:00
|
|
|
* Copyright (C) 2007-2014 Daniel Marjamäki and 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"
|
|
|
|
#include "cppcheck.h"
|
2013-09-04 06:18:22 +02:00
|
|
|
#include "cppcheckexecutor.h"
|
2014-05-23 14:17:39 +02:00
|
|
|
#include <iostream>
|
2011-03-04 23:27:29 +01:00
|
|
|
#ifdef THREADING_MODEL_FORK
|
2012-02-19 17:22:59 +01:00
|
|
|
#include <algorithm>
|
2013-01-13 13:21:46 +01:00
|
|
|
#include <sys/select.h>
|
2009-02-20 20:40:42 +01:00
|
|
|
#include <sys/wait.h>
|
2009-02-19 23:21:18 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2009-02-20 20:53:14 +01:00
|
|
|
#include <cstdlib>
|
2009-03-06 21:46:32 +01:00
|
|
|
#include <cstdio>
|
2010-07-07 14:42:39 +02:00
|
|
|
#include <errno.h>
|
2010-12-30 21:35:53 +01:00
|
|
|
#include <time.h>
|
2011-11-09 21:15:53 +01:00
|
|
|
#include <cstring>
|
2012-04-08 15:22:50 +02:00
|
|
|
#include <sstream>
|
2009-02-20 20:40:42 +01:00
|
|
|
#endif
|
2012-12-12 19:09:37 +01:00
|
|
|
#ifdef THREADING_MODEL_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
|
|
|
#include <errno.h>
|
|
|
|
#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)
|
2012-02-19 17:22:59 +01:00
|
|
|
: _files(files), _settings(settings), _errorLogger(errorLogger), _fileCount(0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2012-12-13 21:03:41 +01:00
|
|
|
#if defined(THREADING_MODEL_FORK)
|
2011-03-05 04:43:22 +01:00
|
|
|
_wpipe = 0;
|
2012-12-13 21:03:41 +01:00
|
|
|
#elif defined(THREADING_MODEL_WIN)
|
|
|
|
_processedFiles = 0;
|
|
|
|
_totalFiles = 0;
|
|
|
|
_processedSize = 0;
|
|
|
|
_totalFileSize = 0;
|
2010-12-09 19:03:41 +01:00
|
|
|
#endif
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadExecutor::~ThreadExecutor()
|
|
|
|
{
|
|
|
|
//dtor
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-02-19 17:22:59 +01:00
|
|
|
void ThreadExecutor::addFileContent(const std::string &path, const std::string &content)
|
|
|
|
{
|
|
|
|
_fileContents[ path ] = content;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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) {
|
2009-02-20 20:40:42 +01:00
|
|
|
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
|
2012-12-27 11:51:12 +01:00
|
|
|
std::exit(0);
|
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) {
|
2009-03-01 21:08:47 +01:00
|
|
|
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
|
2012-12-27 11:51:12 +01:00
|
|
|
std::exit(0);
|
2009-03-01 21:08:47 +01:00
|
|
|
}
|
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
char *buf = new char[len];
|
2011-10-13 20:53:06 +02:00
|
|
|
if (read(rpipe, buf, len) <= 0) {
|
2009-03-01 21:08:47 +01:00
|
|
|
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
|
2012-12-27 11:51:12 +01:00
|
|
|
std::exit(0);
|
2009-03-01 21:08:47 +01:00
|
|
|
}
|
|
|
|
|
2012-01-03 19:51:16 +01:00
|
|
|
if (type == REPORT_OUT) {
|
2009-02-19 23:21:18 +01:00
|
|
|
_errorLogger.reportOut(buf);
|
2012-10-19 19:26:42 +02:00
|
|
|
} else if (type == REPORT_ERROR || type == REPORT_INFO) {
|
2009-02-19 23:21:18 +01:00
|
|
|
ErrorLogger::ErrorMessage msg;
|
|
|
|
msg.deserialize(buf);
|
2009-03-01 20:34:32 +01:00
|
|
|
|
2011-02-16 02:12:15 +01:00
|
|
|
std::string file;
|
|
|
|
unsigned int line(0);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!msg._callStack.empty()) {
|
2011-02-16 02:12:15 +01:00
|
|
|
file = msg._callStack.back().getfile(false);
|
|
|
|
line = msg._callStack.back().line;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!_settings.nomsg.isSuppressed(msg._id, file, line)) {
|
2011-02-16 02:12:15 +01:00
|
|
|
// Alert only about unique errors
|
|
|
|
std::string errmsg = msg.toString(_settings._verbose);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end()) {
|
2011-02-16 02:12:15 +01:00
|
|
|
_errorList.push_back(errmsg);
|
2012-10-19 20:17:34 +02:00
|
|
|
if (type == REPORT_ERROR)
|
|
|
|
_errorLogger.reportErr(msg);
|
|
|
|
else
|
|
|
|
_errorLogger.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;
|
2010-07-07 14:42:39 +02:00
|
|
|
delete [] buf;
|
|
|
|
return -1;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
delete [] buf;
|
2010-07-07 14:42:39 +02:00
|
|
|
return 1;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2014-03-25 18:35:59 +01:00
|
|
|
bool ThreadExecutor::checkLoadAverage(size_t nchilds)
|
|
|
|
{
|
2014-04-05 20:47:02 +02:00
|
|
|
#ifdef __CYGWIN__ // getloadavg() is unsupported on Cygwin.
|
|
|
|
return true;
|
|
|
|
#else
|
2014-03-25 18:35:59 +01:00
|
|
|
if (!nchilds || !_settings._loadAverage) {
|
|
|
|
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;
|
|
|
|
} else if (sample < _settings._loadAverage) {
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
_fileCount = 0;
|
|
|
|
unsigned int result = 0;
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t totalfilesize = 0;
|
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = _files.begin(); i != _files.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;
|
|
|
|
std::map<std::string, std::size_t>::const_iterator i = _files.begin();
|
2012-03-24 21:47:52 +01:00
|
|
|
for (;;) {
|
2010-07-07 14:42:39 +02:00
|
|
|
// Start a new child
|
2014-03-25 18:35:59 +01:00
|
|
|
size_t nchilds = rpipes.size();
|
|
|
|
if (i != _files.end() && nchilds < _settings._jobs && checkLoadAverage(nchilds)) {
|
2011-03-05 04:43:22 +01:00
|
|
|
int pipes[2];
|
2011-10-13 20:53:06 +02:00
|
|
|
if (pipe(pipes) == -1) {
|
2012-12-27 11:51:12 +01:00
|
|
|
std::cerr << "pipe() failed: "<< std::strerror(errno) << std::endl;
|
|
|
|
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) {
|
2012-12-27 11:51:12 +01:00
|
|
|
std::cerr << "fcntl(F_GETFL) failed: "<< std::strerror(errno) << std::endl;
|
|
|
|
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) {
|
2012-12-27 11:51:12 +01:00
|
|
|
std::cerr << "fcntl(F_SETFL) failed: "<< std::strerror(errno) << std::endl;
|
|
|
|
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
|
2012-12-27 11:51:12 +01:00
|
|
|
std::cerr << "Failed to create child process: "<< std::strerror(errno) << std::endl;
|
|
|
|
std::exit(EXIT_FAILURE);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (pid == 0) {
|
2011-03-05 04:43:22 +01:00
|
|
|
close(pipes[0]);
|
|
|
|
_wpipe = pipes[1];
|
|
|
|
|
2011-02-16 02:12:15 +01:00
|
|
|
CppCheck fileChecker(*this, false);
|
2012-04-07 13:55:03 +02:00
|
|
|
fileChecker.settings() = _settings;
|
2011-04-24 18:10:25 +02:00
|
|
|
unsigned int resultOfCheck = 0;
|
2010-07-07 14:42:39 +02:00
|
|
|
|
2013-01-16 15:37:07 +01:00
|
|
|
if (!_fileContents.empty() && _fileContents.find(i->first) != _fileContents.end()) {
|
2010-07-07 14:42:39 +02:00
|
|
|
// File content was given as a string
|
2012-02-19 17:22:59 +01:00
|
|
|
resultOfCheck = fileChecker.check(i->first, _fileContents[ i->first ]);
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-07-07 14:42:39 +02:00
|
|
|
// Read file from a file
|
2012-02-19 17:22:59 +01:00
|
|
|
resultOfCheck = fileChecker.check(i->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());
|
2012-12-27 11:51:12 +01:00
|
|
|
std::exit(0);
|
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]);
|
2012-02-19 17:22:59 +01:00
|
|
|
childFile[pid] = i->first;
|
|
|
|
pipeFile[pipes[0]] = i->first;
|
2011-03-05 04:43:22 +01:00
|
|
|
|
2010-07-07 14:42:39 +02:00
|
|
|
++i;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else 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;
|
|
|
|
int r = select(*std::max_element(rpipes.begin(), rpipes.end()) + 1, &rfds, NULL, NULL, &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);
|
2012-07-08 23:39:46 +02:00
|
|
|
std::map<std::string, std::size_t>::const_iterator fs = _files.find(name);
|
2012-02-19 17:22:59 +01:00
|
|
|
if (fs != _files.end()) {
|
2011-04-19 13:52:32 +02:00
|
|
|
size = fs->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_fileCount++;
|
|
|
|
processedsize += size;
|
2011-04-27 22:27:02 +02:00
|
|
|
if (!_settings._errorsOnly)
|
2012-02-19 17:22:59 +01:00
|
|
|
CppCheckExecutor::reportStatus(_fileCount, _files.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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (WIFSIGNALED(stat)) {
|
2011-03-05 04:43:22 +01:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "Internal error: Child process crashed with signal " << WTERMSIG(stat);
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locations;
|
|
|
|
locations.push_back(ErrorLogger::ErrorMessage::FileLocation(childname, 0));
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locations,
|
|
|
|
Severity::error,
|
|
|
|
oss.str(),
|
2011-04-14 18:02:01 +02:00
|
|
|
"cppcheckError",
|
|
|
|
false);
|
2011-05-22 10:43:38 +02:00
|
|
|
|
|
|
|
if (!_settings.nomsg.isSuppressed(errmsg._id, childname, 0))
|
|
|
|
_errorLogger.reportErr(errmsg);
|
2011-03-05 04:43:22 +01:00
|
|
|
}
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
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);
|
2009-02-19 23:21:18 +01: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);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (write(_wpipe, out, len + 1 + sizeof(len)) <= 0) {
|
2009-05-09 21:40:22 +02:00
|
|
|
delete [] out;
|
|
|
|
out = 0;
|
2009-03-01 21:08:47 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::writeToPipe, Failed to write to pipe" << std::endl;
|
2012-12-27 11:51:12 +01:00
|
|
|
std::exit(0);
|
2009-03-01 21:08:47 +01:00
|
|
|
}
|
2009-05-09 21:40:22 +02:00
|
|
|
|
|
|
|
delete [] out;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportOut(const std::string &outmsg)
|
|
|
|
{
|
2012-01-03 19:51:16 +01:00
|
|
|
writeToPipe(REPORT_OUT, outmsg);
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
|
|
|
|
{
|
2012-01-03 19:51:16 +01:00
|
|
|
writeToPipe(REPORT_ERROR, msg.serialize());
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2012-06-18 23:15:48 +02:00
|
|
|
void ThreadExecutor::reportInfo(const ErrorLogger::ErrorMessage &msg)
|
|
|
|
{
|
2012-10-19 19:26:42 +02:00
|
|
|
writeToPipe(REPORT_INFO, msg.serialize());
|
2012-06-18 23:15:48 +02:00
|
|
|
}
|
|
|
|
|
2012-12-12 19:09:37 +01:00
|
|
|
#elif defined(THREADING_MODEL_WIN)
|
|
|
|
|
|
|
|
void ThreadExecutor::addFileContent(const std::string &path, const std::string &content)
|
|
|
|
{
|
|
|
|
_fileContents[path] = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ThreadExecutor::check()
|
|
|
|
{
|
|
|
|
HANDLE *threadHandles = new HANDLE[_settings._jobs];
|
|
|
|
|
|
|
|
_itNextFile = _files.begin();
|
|
|
|
|
2012-12-13 18:47:13 +01:00
|
|
|
_processedFiles = 0;
|
|
|
|
_processedSize = 0;
|
|
|
|
_totalFiles = _files.size();
|
|
|
|
_totalFileSize = 0;
|
|
|
|
for (std::map<std::string, std::size_t>::const_iterator i = _files.begin(); i != _files.end(); ++i) {
|
|
|
|
_totalFileSize += i->second;
|
|
|
|
}
|
|
|
|
|
2012-12-12 19:09:37 +01:00
|
|
|
InitializeCriticalSection(&_fileSync);
|
|
|
|
InitializeCriticalSection(&_errorSync);
|
|
|
|
InitializeCriticalSection(&_reportSync);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < _settings._jobs; ++i) {
|
|
|
|
threadHandles[i] = (HANDLE)_beginthreadex(NULL, 0, threadProc, this, 0, NULL);
|
|
|
|
if (!threadHandles[i]) {
|
|
|
|
std::cerr << "#### .\nThreadExecutor::check error, errno :" << errno << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD waitResult = WaitForMultipleObjects(_settings._jobs, threadHandles, TRUE, INFINITE);
|
|
|
|
if (waitResult != WAIT_OBJECT_0) {
|
|
|
|
if (waitResult == WAIT_FAILED) {
|
|
|
|
std::cerr << "#### .\nThreadExecutor::check wait failed, result: " << waitResult << " error: " << GetLastError() << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
std::cerr << "#### .\nThreadExecutor::check wait failed, result: " << waitResult << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int result = 0;
|
|
|
|
for (unsigned int i = 0; i < _settings._jobs; ++i) {
|
|
|
|
DWORD exitCode;
|
|
|
|
|
|
|
|
if (!GetExitCodeThread(threadHandles[i], &exitCode)) {
|
|
|
|
std::cerr << "#### .\nThreadExecutor::check get exit code failed, error:" << GetLastError() << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
result += exitCode;
|
|
|
|
|
|
|
|
if (!CloseHandle(threadHandles[i])) {
|
|
|
|
std::cerr << "#### .\nThreadExecutor::check close handle failed, error:" << GetLastError() << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteCriticalSection(&_fileSync);
|
|
|
|
DeleteCriticalSection(&_errorSync);
|
|
|
|
DeleteCriticalSection(&_reportSync);
|
|
|
|
|
|
|
|
delete[] threadHandles;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int __stdcall ThreadExecutor::threadProc(void *args)
|
|
|
|
{
|
|
|
|
unsigned int result = 0;
|
|
|
|
|
|
|
|
ThreadExecutor *threadExecutor = static_cast<ThreadExecutor*>(args);
|
|
|
|
std::map<std::string, std::size_t>::const_iterator &it = threadExecutor->_itNextFile;
|
|
|
|
|
|
|
|
// guard static members of CppCheck against concurrent access
|
|
|
|
EnterCriticalSection(&threadExecutor->_fileSync);
|
|
|
|
|
|
|
|
CppCheck fileChecker(*threadExecutor, false);
|
|
|
|
fileChecker.settings() = threadExecutor->_settings;
|
|
|
|
|
|
|
|
LeaveCriticalSection(&threadExecutor->_fileSync);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
EnterCriticalSection(&threadExecutor->_fileSync);
|
|
|
|
|
|
|
|
if (it == threadExecutor->_files.end()) {
|
|
|
|
LeaveCriticalSection(&threadExecutor->_fileSync);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
2012-12-13 18:47:13 +01:00
|
|
|
const std::string &file = it->first;
|
2013-02-10 07:43:09 +01:00
|
|
|
const std::size_t fileSize = it->second;
|
2013-03-31 10:26:09 +02:00
|
|
|
++it;
|
2012-12-12 19:09:37 +01:00
|
|
|
|
|
|
|
LeaveCriticalSection(&threadExecutor->_fileSync);
|
|
|
|
|
2013-03-31 10:26:09 +02:00
|
|
|
std::map<std::string, std::string>::const_iterator fileContent = threadExecutor->_fileContents.find(file);
|
|
|
|
if (fileContent != threadExecutor->_fileContents.end()) {
|
2012-12-12 19:09:37 +01:00
|
|
|
// File content was given as a string
|
2013-03-31 10:26:09 +02:00
|
|
|
result += fileChecker.check(file, fileContent->second);
|
2012-12-12 19:09:37 +01:00
|
|
|
} else {
|
|
|
|
// Read file from a file
|
|
|
|
result += fileChecker.check(file);
|
|
|
|
}
|
|
|
|
|
2012-12-13 18:47:13 +01:00
|
|
|
EnterCriticalSection(&threadExecutor->_fileSync);
|
|
|
|
|
|
|
|
threadExecutor->_processedSize += fileSize;
|
|
|
|
threadExecutor->_processedFiles++;
|
|
|
|
if (!threadExecutor->_settings._errorsOnly) {
|
|
|
|
EnterCriticalSection(&threadExecutor->_reportSync);
|
|
|
|
CppCheckExecutor::reportStatus(threadExecutor->_processedFiles, threadExecutor->_totalFiles, threadExecutor->_processedSize, threadExecutor->_totalFileSize);
|
|
|
|
LeaveCriticalSection(&threadExecutor->_reportSync);
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&threadExecutor->_fileSync);
|
2012-12-12 19:09:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportOut(const std::string &outmsg)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&_reportSync);
|
|
|
|
|
|
|
|
_errorLogger.reportOut(outmsg);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&_reportSync);
|
|
|
|
}
|
|
|
|
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
|
|
|
|
{
|
|
|
|
report(msg, REPORT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportInfo(const ErrorLogger::ErrorMessage &msg)
|
|
|
|
{
|
|
|
|
report(msg, REPORT_INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::report(const ErrorLogger::ErrorMessage &msg, MessageType msgType)
|
|
|
|
{
|
|
|
|
std::string file;
|
|
|
|
unsigned int line(0);
|
|
|
|
if (!msg._callStack.empty()) {
|
|
|
|
file = msg._callStack.back().getfile(false);
|
|
|
|
line = msg._callStack.back().line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_settings.nomsg.isSuppressed(msg._id, file, line))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Alert only about unique errors
|
|
|
|
bool reportError = false;
|
|
|
|
std::string errmsg = msg.toString(_settings._verbose);
|
|
|
|
|
|
|
|
EnterCriticalSection(&_errorSync);
|
|
|
|
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end()) {
|
|
|
|
_errorList.push_back(errmsg);
|
|
|
|
reportError = true;
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&_errorSync);
|
|
|
|
|
|
|
|
if (reportError) {
|
|
|
|
EnterCriticalSection(&_reportSync);
|
|
|
|
|
|
|
|
switch (msgType) {
|
|
|
|
case REPORT_ERROR:
|
|
|
|
_errorLogger.reportErr(msg);
|
|
|
|
break;
|
|
|
|
case REPORT_INFO:
|
|
|
|
_errorLogger.reportInfo(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&_reportSync);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
#else
|
2012-02-19 17:22:59 +01:00
|
|
|
|
|
|
|
void ThreadExecutor::addFileContent(const std::string &/*path*/, const std::string &/*content*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-02-20 20:40:42 +01:00
|
|
|
unsigned int ThreadExecutor::check()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
|
2009-02-20 20:40:42 +01:00
|
|
|
void ThreadExecutor::reportOut(const std::string &/*outmsg*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &/*msg*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-11 10:26:32 +02:00
|
|
|
void ThreadExecutor::reportInfo(const ErrorLogger::ErrorMessage &/*msg*/)
|
2012-06-18 23:15:48 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
#endif
|