2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2018 Sergey Morozov
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2018-07-30 21:42:47 +03:00
|
|
|
|
|
|
|
|
#include "cppcheckrunner.h"
|
|
|
|
|
#include "cppchecktool.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/hostosinfo.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/messagemanager.h>
|
|
|
|
|
|
2019-05-28 18:59:45 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2023-01-09 18:08:30 +01:00
|
|
|
namespace Cppcheck::Internal {
|
2018-07-30 21:42:47 +03:00
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
CppcheckRunner::CppcheckRunner(CppcheckTool &tool) : m_tool(tool)
|
2018-07-30 21:42:47 +03:00
|
|
|
{
|
2022-06-20 17:23:57 +02:00
|
|
|
if (HostOsInfo::hostOs() == OsTypeLinux) {
|
2023-05-03 16:00:22 +02:00
|
|
|
Process getConf;
|
2021-11-02 13:41:58 +01:00
|
|
|
getConf.setCommand({"getconf", {"ARG_MAX"}});
|
|
|
|
|
getConf.start();
|
2018-07-30 21:42:47 +03:00
|
|
|
getConf.waitForFinished(2000);
|
2023-01-05 17:55:04 +01:00
|
|
|
const QByteArray argMax = getConf.readAllRawStandardOutput().replace("\n", "");
|
2018-07-30 21:42:47 +03:00
|
|
|
m_maxArgumentsLength = std::max(argMax.toInt(), m_maxArgumentsLength);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
m_process.setStdOutLineCallback([this](const QString &line) {
|
2021-06-04 11:41:13 +02:00
|
|
|
m_tool.parseOutputLine(line);
|
|
|
|
|
});
|
2022-06-20 17:23:57 +02:00
|
|
|
m_process.setStdErrLineCallback([this](const QString &line) {
|
2021-06-04 11:41:13 +02:00
|
|
|
m_tool.parseErrorLine(line);
|
|
|
|
|
});
|
|
|
|
|
|
2023-05-03 16:00:22 +02:00
|
|
|
connect(&m_process, &Process::started, &m_tool, &CppcheckTool::startParsing);
|
|
|
|
|
connect(&m_process, &Process::done, this, &CppcheckRunner::handleDone);
|
2018-07-30 21:42:47 +03:00
|
|
|
|
|
|
|
|
m_queueTimer.setSingleShot(true);
|
2018-09-25 21:08:52 +03:00
|
|
|
const int checkDelayInMs = 200;
|
2018-07-30 21:42:47 +03:00
|
|
|
m_queueTimer.setInterval(checkDelayInMs);
|
|
|
|
|
connect(&m_queueTimer, &QTimer::timeout,
|
|
|
|
|
this, &CppcheckRunner::checkQueued);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CppcheckRunner::~CppcheckRunner()
|
|
|
|
|
{
|
|
|
|
|
stop();
|
|
|
|
|
m_queueTimer.stop();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 16:58:46 +02:00
|
|
|
void CppcheckRunner::reconfigure(const FilePath &binary, const QString &arguments)
|
2018-07-30 21:42:47 +03:00
|
|
|
{
|
|
|
|
|
m_binary = binary;
|
|
|
|
|
m_arguments = arguments;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
void CppcheckRunner::addToQueue(const FilePaths &files,
|
2018-07-30 21:42:47 +03:00
|
|
|
const QString &additionalArguments)
|
|
|
|
|
{
|
2022-06-20 17:23:57 +02:00
|
|
|
FilePaths &existing = m_queue[additionalArguments];
|
2018-07-30 21:42:47 +03:00
|
|
|
if (existing.isEmpty()) {
|
|
|
|
|
existing = files;
|
|
|
|
|
} else {
|
|
|
|
|
std::copy_if(files.cbegin(), files.cend(), std::back_inserter(existing),
|
2022-06-20 17:23:57 +02:00
|
|
|
[&existing](const FilePath &file) { return !existing.contains(file); });
|
2018-07-30 21:42:47 +03:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
if (m_process.isRunning()) {
|
2018-09-22 14:22:03 +03:00
|
|
|
stop(existing);
|
2018-07-30 21:42:47 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_queueTimer.start();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
void CppcheckRunner::stop(const FilePaths &files)
|
2018-07-30 21:42:47 +03:00
|
|
|
{
|
2022-06-20 17:23:57 +02:00
|
|
|
if (!m_process.isRunning())
|
2018-09-22 14:22:03 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (files.isEmpty() || m_currentFiles == files)
|
2022-06-20 17:23:57 +02:00
|
|
|
m_process.stop();
|
2018-07-30 21:42:47 +03:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
void CppcheckRunner::removeFromQueue(const FilePaths &files)
|
2018-07-30 21:42:47 +03:00
|
|
|
{
|
|
|
|
|
if (m_queue.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (files.isEmpty()) {
|
|
|
|
|
m_queue.clear();
|
|
|
|
|
} else {
|
|
|
|
|
for (auto it = m_queue.begin(), end = m_queue.end(); it != end;) {
|
2022-06-20 17:23:57 +02:00
|
|
|
for (const FilePath &file : files)
|
2018-07-30 21:42:47 +03:00
|
|
|
it.value().removeOne(file);
|
|
|
|
|
it = !it.value().isEmpty() ? ++it : m_queue.erase(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
const FilePaths &CppcheckRunner::currentFiles() const
|
2018-07-30 21:42:47 +03:00
|
|
|
{
|
|
|
|
|
return m_currentFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CppcheckRunner::currentCommand() const
|
|
|
|
|
{
|
2022-06-20 17:23:57 +02:00
|
|
|
return m_process.commandLine().toUserOutput();
|
2018-07-30 21:42:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppcheckRunner::checkQueued()
|
|
|
|
|
{
|
2022-10-26 11:44:11 +02:00
|
|
|
if (m_queue.isEmpty() || !m_binary.isExecutableFile())
|
2018-07-30 21:42:47 +03:00
|
|
|
return;
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
FilePaths files = m_queue.begin().value();
|
2018-07-30 21:42:47 +03:00
|
|
|
QString arguments = m_arguments + ' ' + m_queue.begin().key();
|
|
|
|
|
m_currentFiles.clear();
|
|
|
|
|
int argumentsLength = arguments.length();
|
|
|
|
|
while (!files.isEmpty()) {
|
2019-05-10 11:48:14 +02:00
|
|
|
argumentsLength += files.first().toString().size() + 1; // +1 for separator
|
2018-07-30 21:42:47 +03:00
|
|
|
if (argumentsLength >= m_maxArgumentsLength)
|
|
|
|
|
break;
|
|
|
|
|
m_currentFiles.push_back(files.first());
|
|
|
|
|
arguments += ' ' + files.first().toString();
|
|
|
|
|
files.pop_front();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (files.isEmpty())
|
|
|
|
|
m_queue.erase(m_queue.begin());
|
|
|
|
|
else
|
|
|
|
|
m_queue.begin().value() = files;
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
m_process.setCommand(CommandLine(m_binary, arguments, CommandLine::Raw));
|
|
|
|
|
m_process.start();
|
2018-07-30 21:42:47 +03:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
void CppcheckRunner::handleDone()
|
2018-07-30 21:42:47 +03:00
|
|
|
{
|
2022-06-20 17:23:57 +02:00
|
|
|
if (m_process.result() == ProcessResult::FinishedWithSuccess)
|
2018-07-30 21:42:47 +03:00
|
|
|
m_tool.finishParsing();
|
2022-06-20 17:23:57 +02:00
|
|
|
else
|
|
|
|
|
Core::MessageManager::writeSilently(m_process.exitMessage());
|
|
|
|
|
|
2018-07-30 21:42:47 +03:00
|
|
|
m_currentFiles.clear();
|
2022-06-20 17:23:57 +02:00
|
|
|
m_process.close();
|
2018-07-30 21:42:47 +03:00
|
|
|
|
|
|
|
|
if (!m_queue.isEmpty())
|
|
|
|
|
checkQueued();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 18:08:30 +01:00
|
|
|
} // Cppcheck::Internal
|