2009-03-01 21:44:42 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2016-01-01 14:34:45 +01:00
|
|
|
* Copyright (C) 2007-2016 Cppcheck team.
|
2009-03-01 21:44:42 +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-03-01 21:44:42 +01:00
|
|
|
*/
|
|
|
|
|
2010-10-31 12:16:55 +01:00
|
|
|
#include <QObject>
|
2012-02-14 21:16:11 +01:00
|
|
|
#include <QFileInfo>
|
2010-10-31 12:16:55 +01:00
|
|
|
#include <QStringList>
|
2009-03-01 21:44:42 +01:00
|
|
|
#include <QDebug>
|
2012-10-07 12:25:34 +02:00
|
|
|
#include "common.h"
|
2010-10-31 12:16:55 +01:00
|
|
|
#include "settings.h"
|
2012-10-07 12:25:34 +02:00
|
|
|
#include "checkthread.h"
|
2010-10-31 12:16:55 +01:00
|
|
|
#include "threadhandler.h"
|
|
|
|
#include "resultsview.h"
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2009-06-20 18:55:23 +02:00
|
|
|
ThreadHandler::ThreadHandler(QObject *parent) :
|
2010-04-15 20:08:51 +02:00
|
|
|
QObject(parent),
|
2010-09-01 08:12:24 +02:00
|
|
|
mScanDuration(0),
|
2016-11-19 20:38:50 +01:00
|
|
|
mRunningThreadCount(0),
|
|
|
|
mAnalyseWholeProgram(false)
|
|
|
|
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2017-07-28 12:39:28 +02:00
|
|
|
setThreadCount(1);
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadHandler::~ThreadHandler()
|
|
|
|
{
|
2017-07-28 13:13:10 +02:00
|
|
|
removeThreads();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::clearFiles()
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2009-03-22 16:42:00 +01:00
|
|
|
mLastFiles.clear();
|
2017-07-28 12:39:28 +02:00
|
|
|
mResults.clearFiles();
|
2016-11-19 20:38:50 +01:00
|
|
|
mAnalyseWholeProgram = false;
|
2017-08-03 12:30:28 +02:00
|
|
|
mAddons.clear();
|
2017-08-11 07:45:29 +02:00
|
|
|
mSuppressions.clear();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::setFiles(const QStringList &files)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2017-07-28 12:39:28 +02:00
|
|
|
mResults.setFiles(files);
|
2009-03-22 16:42:00 +01:00
|
|
|
mLastFiles = files;
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::setProject(const ImportProject &prj)
|
2016-08-18 21:58:50 +02:00
|
|
|
{
|
2017-07-28 12:39:28 +02:00
|
|
|
mResults.setProject(prj);
|
2016-08-18 21:58:50 +02:00
|
|
|
mLastFiles.clear();
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::setCheckFiles(bool all)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2015-12-23 10:28:07 +01:00
|
|
|
if (mRunningThreadCount == 0) {
|
2017-07-28 12:39:28 +02:00
|
|
|
mResults.setFiles(getReCheckFiles(all));
|
2009-03-22 16:42:00 +01:00
|
|
|
}
|
2015-12-23 10:28:07 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::setCheckFiles(QStringList files)
|
2015-12-23 10:28:07 +01:00
|
|
|
{
|
|
|
|
if (mRunningThreadCount == 0) {
|
2017-07-28 12:39:28 +02:00
|
|
|
mResults.setFiles(files);
|
2015-12-23 10:28:07 +01:00
|
|
|
}
|
|
|
|
}
|
2009-03-22 16:42:00 +01:00
|
|
|
|
2017-08-03 22:12:12 +02:00
|
|
|
void ThreadHandler::check(const Settings &settings)
|
2015-12-23 10:28:07 +01:00
|
|
|
{
|
2017-07-28 12:39:28 +02:00
|
|
|
if (mResults.getFileCount() == 0 || mRunningThreadCount > 0 || settings.jobs == 0) {
|
2009-03-02 20:56:51 +01:00
|
|
|
qDebug() << "Can't start checking if there's no files to check or if check is in progress.";
|
2017-07-28 12:39:28 +02:00
|
|
|
emit done();
|
2009-03-01 21:44:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
setThreadCount(settings.jobs);
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2009-03-02 20:56:51 +01:00
|
|
|
mRunningThreadCount = mThreads.size();
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
if (mResults.getFileCount() < mRunningThreadCount) {
|
|
|
|
mRunningThreadCount = mResults.getFileCount();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = 0; i < mRunningThreadCount; i++) {
|
2017-08-03 12:30:28 +02:00
|
|
|
mThreads[i]->setAddons(mAddons);
|
2017-08-11 07:45:29 +02:00
|
|
|
mThreads[i]->setSuppressions(mSuppressions);
|
2017-08-07 23:00:24 +02:00
|
|
|
mThreads[i]->setVsIncludePaths(mVsIncludePaths);
|
2017-08-04 20:08:01 +02:00
|
|
|
mThreads[i]->setClangPath(mClangPath);
|
2017-08-03 12:30:28 +02:00
|
|
|
mThreads[i]->setDataDir(mDataDir);
|
2017-07-28 05:26:11 +02:00
|
|
|
mThreads[i]->check(settings);
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
2010-09-01 08:12:24 +02:00
|
|
|
|
2012-02-14 21:16:11 +01:00
|
|
|
// Date and time when checking starts..
|
|
|
|
mCheckStartTime = QDateTime::currentDateTime();
|
|
|
|
|
2016-11-19 20:38:50 +01:00
|
|
|
mAnalyseWholeProgram = true;
|
|
|
|
|
2010-09-01 08:12:24 +02:00
|
|
|
mTime.start();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
bool ThreadHandler::isChecking() const
|
2009-06-04 16:02:35 +02:00
|
|
|
{
|
|
|
|
return mRunningThreadCount > 0;
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::setThreadCount(const int count)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (mRunningThreadCount > 0 ||
|
|
|
|
count == mThreads.size() ||
|
2011-10-13 20:53:06 +02:00
|
|
|
count <= 0) {
|
2009-03-01 21:44:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove unused old threads
|
2017-07-28 13:13:10 +02:00
|
|
|
removeThreads();
|
2009-03-02 20:56:51 +01:00
|
|
|
//Create new threads
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = mThreads.size(); i < count; i++) {
|
2009-03-02 20:56:51 +01:00
|
|
|
mThreads << new CheckThread(mResults);
|
2017-07-31 13:46:23 +02:00
|
|
|
connect(mThreads.last(), &CheckThread::done,
|
|
|
|
this, &ThreadHandler::threadDone);
|
|
|
|
connect(mThreads.last(), &CheckThread::fileChecked,
|
|
|
|
&mResults, &ThreadResult::fileChecked);
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
2009-03-02 20:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-28 13:13:10 +02:00
|
|
|
void ThreadHandler::removeThreads()
|
2009-03-02 20:56:51 +01:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = 0; i < mThreads.size(); i++) {
|
2009-03-02 20:56:51 +01:00
|
|
|
mThreads[i]->terminate();
|
2017-07-31 13:46:23 +02:00
|
|
|
disconnect(mThreads[i], &CheckThread::done,
|
2017-08-01 14:33:12 +02:00
|
|
|
this, &ThreadHandler::threadDone);
|
2017-07-31 13:46:23 +02:00
|
|
|
disconnect(mThreads[i], &CheckThread::fileChecked,
|
2017-08-01 14:33:12 +02:00
|
|
|
&mResults, &ThreadResult::fileChecked);
|
2009-03-02 20:56:51 +01:00
|
|
|
delete mThreads[i];
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
2009-03-02 20:56:51 +01:00
|
|
|
|
|
|
|
mThreads.clear();
|
2016-11-19 20:38:50 +01:00
|
|
|
mAnalyseWholeProgram = false;
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::threadDone()
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2016-11-19 20:38:50 +01:00
|
|
|
if (mRunningThreadCount == 1 && mAnalyseWholeProgram) {
|
2017-07-28 05:26:11 +02:00
|
|
|
mThreads[0]->analyseWholeProgram(mLastFiles);
|
2016-11-19 20:38:50 +01:00
|
|
|
mAnalyseWholeProgram = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-01 21:44:42 +01:00
|
|
|
mRunningThreadCount--;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (mRunningThreadCount == 0) {
|
2017-07-28 12:39:28 +02:00
|
|
|
emit done();
|
2010-09-01 08:12:24 +02:00
|
|
|
|
|
|
|
mScanDuration = mTime.elapsed();
|
2012-02-14 21:16:11 +01:00
|
|
|
|
|
|
|
// Set date/time used by the recheck
|
|
|
|
if (!mCheckStartTime.isNull()) {
|
|
|
|
mLastCheckTime = mCheckStartTime;
|
|
|
|
mCheckStartTime = QDateTime();
|
|
|
|
}
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::stop()
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2012-02-14 21:16:11 +01:00
|
|
|
mCheckStartTime = QDateTime();
|
2016-11-19 20:38:50 +01:00
|
|
|
mAnalyseWholeProgram = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = 0; i < mThreads.size(); i++) {
|
2009-06-02 01:01:53 +02:00
|
|
|
mThreads[i]->stop();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::initialize(ResultsView *view)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2017-07-31 13:52:03 +02:00
|
|
|
connect(&mResults, &ThreadResult::progress,
|
|
|
|
view, &ResultsView::progress);
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2017-07-31 13:52:03 +02:00
|
|
|
connect(&mResults, &ThreadResult::error,
|
|
|
|
view, &ResultsView::error);
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
connect(&mResults, SIGNAL(log(const QString &)),
|
2017-07-28 12:22:15 +02:00
|
|
|
parent(), SLOT(log(const QString &)));
|
2010-08-28 19:37:21 +02:00
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
connect(&mResults, SIGNAL(debugError(const ErrorItem &)),
|
2017-07-28 12:22:15 +02:00
|
|
|
parent(), SLOT(debugError(const ErrorItem &)));
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::loadSettings(QSettings &settings)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2017-07-28 12:39:28 +02:00
|
|
|
setThreadCount(settings.value(SETTINGS_CHECK_THREADS, 1).toInt());
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::saveSettings(QSettings &settings) const
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2009-07-02 10:32:29 +02:00
|
|
|
settings.setValue(SETTINGS_CHECK_THREADS, mThreads.size());
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
bool ThreadHandler::hasPreviousFiles() const
|
2009-10-13 10:19:00 +02:00
|
|
|
{
|
2013-01-16 15:37:07 +01:00
|
|
|
return !mLastFiles.isEmpty();
|
2009-10-13 10:19:00 +02:00
|
|
|
}
|
2010-07-07 11:25:47 +02:00
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
int ThreadHandler::getPreviousFilesCount() const
|
2010-07-07 11:25:47 +02:00
|
|
|
{
|
|
|
|
return mLastFiles.size();
|
|
|
|
}
|
2010-09-01 08:12:24 +02:00
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
int ThreadHandler::getPreviousScanDuration() const
|
2010-09-01 08:12:24 +02:00
|
|
|
{
|
|
|
|
return mScanDuration;
|
|
|
|
}
|
2012-02-14 21:16:11 +01:00
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
QStringList ThreadHandler::getReCheckFiles(bool all) const
|
2012-02-14 21:16:11 +01:00
|
|
|
{
|
2015-12-23 10:28:07 +01:00
|
|
|
if (mLastCheckTime.isNull() || all)
|
2012-02-14 21:16:11 +01:00
|
|
|
return mLastFiles;
|
|
|
|
|
|
|
|
std::set<QString> modified;
|
|
|
|
std::set<QString> unmodified;
|
|
|
|
|
|
|
|
QStringList files;
|
|
|
|
for (int i = 0; i < mLastFiles.size(); ++i) {
|
2017-07-28 12:39:28 +02:00
|
|
|
if (needsReCheck(mLastFiles[i], modified, unmodified))
|
2012-02-14 21:16:11 +01:00
|
|
|
files.push_back(mLastFiles[i]);
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
bool ThreadHandler::needsReCheck(const QString &filename, std::set<QString> &modified, std::set<QString> &unmodified) const
|
2012-02-14 21:16:11 +01:00
|
|
|
{
|
|
|
|
if (modified.find(filename) != modified.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (unmodified.find(filename) != unmodified.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (QFileInfo(filename).lastModified() > mLastCheckTime) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse included files recursively
|
|
|
|
QFile f(filename);
|
|
|
|
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// prevent recursion..
|
|
|
|
unmodified.insert(filename);
|
|
|
|
|
|
|
|
QTextStream in(&f);
|
|
|
|
while (!in.atEnd()) {
|
|
|
|
QString line = in.readLine();
|
|
|
|
if (line.startsWith("#include \"")) {
|
|
|
|
line.remove(0,10);
|
|
|
|
int i = line.indexOf("\"");
|
|
|
|
if (i > 0) {
|
|
|
|
line.remove(i,line.length());
|
|
|
|
line = QFileInfo(filename).absolutePath() + "/" + line;
|
2017-07-28 12:39:28 +02:00
|
|
|
if (needsReCheck(line, modified, unmodified)) {
|
2012-02-14 21:16:11 +01:00
|
|
|
modified.insert(line);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-23 10:28:07 +01:00
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
QDateTime ThreadHandler::getCheckStartTime() const
|
2015-12-23 10:28:07 +01:00
|
|
|
{
|
|
|
|
return mCheckStartTime;
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:39:28 +02:00
|
|
|
void ThreadHandler::setCheckStartTime(QDateTime checkStartTime)
|
2015-12-23 10:28:07 +01:00
|
|
|
{
|
|
|
|
mCheckStartTime = checkStartTime;
|
|
|
|
}
|