2009-02-19 23:21:18 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 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"
|
|
|
|
#include <iostream>
|
2009-03-01 20:56:27 +01:00
|
|
|
#include <algorithm>
|
2010-01-14 22:01:33 +01:00
|
|
|
#if (defined(__GNUC__) || defined(__sun)) && !defined(__MINGW32__)
|
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>
|
|
|
|
#include <cstring>
|
2009-03-06 21:46:32 +01:00
|
|
|
#include <cstdio>
|
2009-02-20 20:40:42 +01:00
|
|
|
#endif
|
2009-02-19 23:21:18 +01:00
|
|
|
|
|
|
|
ThreadExecutor::ThreadExecutor(const std::vector<std::string> &filenames, const Settings &settings, ErrorLogger &errorLogger)
|
2010-04-02 07:30:58 +02:00
|
|
|
: _filenames(filenames), _settings(settings), _errorLogger(errorLogger), _fileCount(0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadExecutor::~ThreadExecutor()
|
|
|
|
{
|
|
|
|
//dtor
|
|
|
|
}
|
|
|
|
|
2009-02-20 20:40:42 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2010-01-14 22:01:33 +01:00
|
|
|
////// This code is for __GNUC__ and __sun only ///////////////////////////////
|
2009-02-20 20:40:42 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-01-14 22:01:33 +01:00
|
|
|
#if (defined(__GNUC__) || defined(__sun)) && !defined(__MINGW32__)
|
2009-02-20 20:40:42 +01:00
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
bool ThreadExecutor::handleRead(unsigned int &result)
|
|
|
|
{
|
|
|
|
char type = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (read(_pipe[0], &type, 1) <= 0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type != '1' && type != '2' && type != '3')
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2009-02-20 20:40:42 +01:00
|
|
|
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
|
2009-02-19 23:21:18 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int len = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (read(_pipe[0], &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;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
char *buf = new char[len];
|
2010-04-02 07:30:58 +02:00
|
|
|
if (read(_pipe[0], 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;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type == '1')
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
_errorLogger.reportOut(buf);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (type == '2')
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
ErrorLogger::ErrorMessage msg;
|
|
|
|
msg.deserialize(buf);
|
2009-03-01 20:34:32 +01:00
|
|
|
|
|
|
|
// Alert only about unique errors
|
2009-07-10 22:38:26 +02:00
|
|
|
std::string errmsg = msg.toText();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end())
|
2009-03-01 20:34:32 +01:00
|
|
|
{
|
|
|
|
_errorList.push_back(errmsg);
|
|
|
|
_errorLogger.reportErr(msg);
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (type == '3')
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
_fileCount++;
|
|
|
|
std::istringstream iss(buf);
|
|
|
|
unsigned int fileResult = 0;
|
|
|
|
iss >> fileResult;
|
|
|
|
result += fileResult;
|
|
|
|
_errorLogger.reportStatus(_fileCount, _filenames.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] buf;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ThreadExecutor::check()
|
|
|
|
{
|
|
|
|
_fileCount = 0;
|
|
|
|
unsigned int result = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pipe(_pipe) == -1)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
perror("pipe");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int flags = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((flags = fcntl(_pipe[0], F_GETFL, 0)) < 0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
perror("fcntl");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (fcntl(_pipe[0], F_SETFL, flags | O_NONBLOCK) < 0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
perror("fcntl");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int childCount = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < _filenames.size(); i++)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
// Keep only wanted amount of child processes running at a time.
|
2010-04-02 07:30:58 +02:00
|
|
|
if (childCount >= _settings._jobs)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (handleRead(result))
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int stat = 0;
|
|
|
|
waitpid(0, &stat, 0);
|
|
|
|
--childCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pid < 0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
// Error
|
|
|
|
std::cerr << "Failed to create child process" << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (pid == 0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
CppCheck fileChecker(*this);
|
|
|
|
fileChecker.settings(_settings);
|
|
|
|
fileChecker.addFile(_filenames[i]);
|
2010-04-09 21:40:37 +02:00
|
|
|
unsigned int resultOfCheck = fileChecker.check();
|
2009-02-19 23:21:18 +01:00
|
|
|
std::ostringstream oss;
|
2010-04-09 21:40:37 +02:00
|
|
|
oss << resultOfCheck;
|
2009-02-19 23:21:18 +01:00
|
|
|
writeToPipe('3', oss.str());
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
++childCount;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (childCount > 0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
int stat = 0;
|
|
|
|
waitpid(0, &stat, 0);
|
|
|
|
--childCount;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (handleRead(result))
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::writeToPipe(char type, const std::string &data)
|
|
|
|
{
|
|
|
|
unsigned int len = data.length() + 1;
|
|
|
|
char *out = new char[ len + 1 + sizeof(len)];
|
|
|
|
out[0] = 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);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (write(_pipe[1], out, len + 1 + sizeof(len)) <= 0)
|
2009-03-01 21:08:47 +01:00
|
|
|
{
|
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;
|
|
|
|
exit(0);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
writeToPipe('1', outmsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
|
|
|
|
{
|
|
|
|
writeToPipe('2', msg.serialize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportStatus(unsigned int /*index*/, unsigned int /*max*/)
|
|
|
|
{
|
|
|
|
// Not used
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
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*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportStatus(unsigned int /*index*/, unsigned int /*max*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
#endif
|