2018-07-30 21:42:47 +03:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2018 Sergey Morozov
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
2018-07-30 21:42:47 +03:00
|
|
|
namespace Cppcheck {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
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) {
|
2021-11-02 13:41:58 +01:00
|
|
|
QtcProcess getConf;
|
|
|
|
|
getConf.setCommand({"getconf", {"ARG_MAX"}});
|
|
|
|
|
getConf.start();
|
2018-07-30 21:42:47 +03:00
|
|
|
getConf.waitForFinished(2000);
|
2018-09-25 21:08:52 +03:00
|
|
|
const QByteArray argMax = getConf.readAllStandardOutput().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);
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-20 17:23:57 +02:00
|
|
|
connect(&m_process, &QtcProcess::started, &m_tool, &CppcheckTool::startParsing);
|
|
|
|
|
connect(&m_process, &QtcProcess::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()
|
|
|
|
|
{
|
|
|
|
|
if (m_queue.isEmpty() || m_binary.isEmpty())
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Cppcheck
|