<commit_before>#include "dcdsupport.h"

#include "dlangdebughelper.h"
#include <dlangoptionspage.h>

#include <stdexcept>

#include <QProcess>
#include <QTextStream>
#include <QDebug>

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
#include <cpptools/cppmodelmanager.h>

using namespace Dcd;

DcdClient::DcdClient(const QString &projectName, const QString &processName, int port, QObject *parent)
    : QObject(parent), m_projectName(projectName), m_port(port), m_processName(processName)
{
    m_portArguments << QLatin1String("--port") << QString::number(port);
}

const QString &DcdClient::projectName() const
{
    return m_projectName;
}

void DcdClient::setOutputFile(const QString &filePath)
{
    m_filePath = filePath;
}

void startProcess(QProcess &p, const QString &processName, const QStringList &args,
                  const QString &filePath, QIODevice::OpenMode mode = QIODevice::ReadWrite)
{
    if (p.state() != QProcess::NotRunning) {
        throw std::runtime_error("process is already running");
    }
    if (!filePath.isEmpty()) {
        p.setStandardOutputFile(filePath, QIODevice::Append | QIODevice::Unbuffered);
        p.setStandardErrorFile(filePath, QIODevice::Append | QIODevice::Unbuffered);
    }
    p.start(processName, args, mode);
    if (!p.waitForStarted(1000)) {
        throw std::runtime_error("process start timeout");
    }
}

void waitForFinished(QProcess &p)
{
    if (!p.waitForFinished(1000)) {
        throw std::runtime_error("process finish timeout");
    }
    if (p.exitStatus() != QProcess::NormalExit || p.exitCode() != 0) {
        throw std::runtime_error(p.readAllStandardError().data());
    }
}

void DcdClient::complete(const QString &filePath, int position, CompletionList &result)
{
    DEBUG_GUARD("");
    QStringList args = m_portArguments;
    args << QLatin1String("-c") + QString::number(position) << filePath;
    QProcess process;
    startProcess(process, m_processName, args, m_filePath);
    waitForFinished(process);
    QByteArray array(process.readAllStandardOutput());
    return parseOutput(array, result);
}

void DcdClient::completeFromArray(const QString &array, int position, DcdClient::CompletionList &result)
{
    DEBUG_GUARD(QString::number(position));
    QStringList args = m_portArguments;
    args << QLatin1String("-c") + QString::number(position);
    QProcess process;
    startProcess(process, m_processName, args, m_filePath);
    process.write(array.toLatin1());
    if (!process.waitForBytesWritten(5000)) {
        throw std::runtime_error("process writing data timeout");
    }
    process.closeWriteChannel();
    waitForFinished(process);
    QByteArray output(process.readAllStandardOutput());
    return parseOutput(output, result);
}

void DcdClient::appendIncludePath(const QString &includePath)
{
    DEBUG_GUARD("");
    QStringList args = m_portArguments;
    args << QLatin1String("-I") + includePath;
    QProcess process;
    startProcess(process, m_processName, args, m_filePath);
    waitForFinished(process);
}

void DcdClient::findSymbolLocation(const QString &array, int position, DcdClient::Location &result)
{
    DEBUG_GUARD(QString::number(position));
    QStringList args = m_portArguments;
    args << QLatin1String("-c") + QString::number(position) << "-l";
    QProcess process;
    startProcess(process, m_processName, args, m_filePath);
    process.write(array.toLatin1());
    if (!process.waitForBytesWritten(2000)) {
        throw std::runtime_error("process writing data timeout");
    }
    process.closeWriteChannel();
    waitForFinished(process);
    QString str(process.readAllStandardOutput());
    QStringList list = str.split('\t');
    result = list.size() == 2 ? Location(list.front(), list.back().toInt()) : Location(QString(), 0);
}

void DcdClient::getDocumentationComments(const QString &array, int position, QStringList &result)
{
    DEBUG_GUARD(QString::number(position));
    QStringList args = m_portArguments;
    args << QLatin1String("-c") + QString::number(position) << "-d";
    QProcess process;<commit_msg>Dcd processes extra logging *Process arguments logging<commit_after>#include "dcdsupport.h"

#include "dlangdebughelper.h"
#include <dlangoptionspage.h>

#include <stdexcept>

#include <QProcess>
#include <QTextStream>
#include <QDebug>

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
#include <cpptools/cppmodelmanager.h>

using namespace Dcd;

DcdClient::DcdClient(const QString &projectName, const QString &processName, int port, QObject *parent)
    : QObject(parent), m_projectName(projectName), m_port(port), m_processName(processName)
{
    m_portArguments << QLatin1String("--port") << QString::number(port);
}

const QString &DcdClient::projectName() const
{
    return m_projectName;
}

void DcdClient::setOutputFile(const QString &filePath)
{
    m_filePath = filePath;
}

void startProcess(QProcess &p, const QString &processName, const QStringList &args,
                  const QString &filePath, QIODevice::OpenMode mode = QIODevice::ReadWrite)
{
    if (p.state() != QProcess::NotRunning) {
        throw std::runtime_error("process is already running");
    }
    if (!filePath.isEmpty()) {
        p.setStandardOutputFile(filePath, QIODevice::Append | QIODevice::Unbuffered);
        p.setStandardErrorFile(filePath, QIODevice::Append | QIODevice::Unbuffered);
    }
    qDebug() << processName << " process " << args;
    p.start(processName, args, mode);
    if (!p.waitForStarted(1000)) {
        throw std::runtime_error("process start timeout");
    }
}

void waitForFinished(QProcess &p)
{
    if (!p.waitForFinished(1000)) {
        throw std::runtime_error("process finish timeout");
    }
    if (p.exitStatus() != QProcess::NormalExit || p.exitCode() != 0) {
        throw std::runtime_error(p.readAllStandardError().data());
    }
}

void DcdClient::complete(const QString &filePath, int position, CompletionList &result)
{
    DEBUG_GUARD("");
    QStringList args = m_portArguments;
    args << QLatin1String("-c") + QString::number(position) << filePath;
    qDebug() << "dcd-client process " << args;
    QProcess process;
    startProcess(process, m_processName, args, m_filePath);
    waitForFinished(process);
    QByteArray array(process.readAllStandardOutput());
    return parseOutput(array, result);
}

void DcdClient::completeFromArray(const QString &array, int position, DcdClient::CompletionList &result)
{
    DEBUG_GUARD(QString::number(position));
    QStringList args = m_portArguments;
    args << QLatin1String("-c") + QString::number(position);
    QProcess process;
    startProcess(process, m_processName, args, m_filePath);
    process.write(array.toLatin1());
    if (!process.waitForBytesWritten(5000)) {
        throw std::runtime_error("process writing data timeout");
    }
    process.closeWriteChannel();
    waitForFinished(process);
    QByteArray output(process.readAllStandardOutput());
    return parseOutput(output, result);
}

void DcdClient::appendIncludePath(const QString &includePath)
{
    DEBUG_GUARD("");
    QStringList args = m_portArguments;
    args << QLatin1String("-I") + includePath;
    qDebug() << "dcd-client process " << args;
    QProcess process;
    startProcess(process, m_processName, args, m_filePath);
    waitForFinished(process);
}

void DcdClient::findSymbolLocation(const QString &array, int position, DcdClient::Location &result)
{
    DEBUG_GUARD(QString::number(position));
    QStringList args = m_portArguments;
    args << QLatin1String("-c") + QString::number(position) << "-l";
    qDebug() << "dcd-client process " << args;
    QProcess process;
    startProcess(process, m_processName, args, m_filePath);
    process.write(array.toLatin1());
    if (!process.waitForBytesWritten(2000)) {
        throw std::runtime_error("process writing data timeout");
    }
    process.closeWriteChannel();
    waitForFinished(process);
    QString str(process.readAllStandardOutput());
    QStringList list = str.split('\t');
    result = list.size() == 2 ? Location(list.front(), list.back().toInt()) : Location(QString(), 0);
}

void DcdClient::getDocumentationComments(const QString &array, int position, QStringList &result)
{
    DEBUG_GUARD(QString::number(position));
    QStringList args = m_portArguments;
    args << QLatin1String("-c") + QString::number(position) << "-d";
    qDebug() << "dcd-client process " << args;
    QProcess process;
{'fraction_non_alphanumeric': 0.13559721730927957, 'fraction_numerical': 0.006367173682348779, 'mean_word_length': 4.833562585969739, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2170385', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2622, 'n_words': 529}