<commit_before>#include <QtGlobal>
#include <QFileInfo>
#include <QDir>
#include <QProcess>
#include <QtDebug>


void runModelMoc(const QString& header, const QStringList& arguments)
{
    QFile inf(header);
    if(!inf.open(QFile::ReadOnly))
        qFatal("Could not open header file for reading");

    QTextStream in(&inf);

    QProcess p;
    p.setProcessChannelMode( QProcess::ForwardedChannels );
    p.start(QString(QT_MOC_PATH), arguments, QIODevice::WriteOnly);
    if(!p.waitForStarted()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }

    QString spaces = "\\s*";
    QString comma = ",";
    QString colon = ":";
    QString parenO = "\\(";
    QString parenC = "\\)";
    QString emptyParen = "\\(\\)";
    QString angleO = "<";
    QString angleC = ">";
    QString braceO = "\\{";
    QString nameSpace = "(?:\\w+::)";
    QString typeName = "\\w+";
    QString pointer = "\\*";
    QString templateName = "\\w+";
    QString plainParam = "(\\w+)";
    QString boolParam = "(true|false)";
    QString anyParam = "(.+)";

    QRegExp propertyRegExp(
            spaces+
            "M_MODEL_PROPERTY"+
            spaces+parenO+spaces+
                "("+
                    "(?:"+
                         nameSpace+"{,1}"+
                         typeName+
                         spaces+
                         pointer+"{,1}"+
                    ")"+
                    "|"+
                    "(?:"+
                         templateName+
                         angleO+
                             spaces+
                             typeName+
                             spaces+
                             pointer+"{,1}"+
                             spaces+
                         angleC+
                     ")"+
                  ")"+
                      spaces+comma+spaces+
                  plainParam+
                      spaces+comma+spaces+
                  plainParam+
                      spaces+comma+spaces+
                  plainParam+
                      spaces+comma+spaces+
                  anyParam+
              spaces+parenC+spaces );

    QRegExp propertyPtrRegExp(
        spaces+
        "M_MODEL_PTR_PROPERTY"+
        spaces+parenO+spaces+
            "("+
                "(?:"+
                    nameSpace+"{,1}"+
                    typeName+
                    spaces+
                    pointer+"{,1}"+
                    spaces+
                ")"+
                "|"+
                "(?:"+
                    templateName+
                    angleO+
                        spaces+
                        typeName+
                        spaces+
                        pointer+"{,1}"+
                        spaces+
                    angleC+
                ")"+
            ")"+
                spaces+comma+spaces+
            plainParam+
                spaces+comma+spaces+
            plainParam+
                spaces+comma+spaces+
            boolParam+
                spaces+comma+spaces+
            anyParam+
        spaces+parenC+spaces );

    QString line;
    while(true) {
        line = in.readLine();
        if(line.isNull()) {
            break;
        }

        line.replace(propertyRegExp, "    Q_PROPERTY(\\1 \\2 READ \\2 WRITE set\\3)");
        line.replace(propertyPtrRegExp, "    Q_PROPERTY(\\1 \\2 READ \\2 WRITE set\\3)");
        p.write(QString(line + "\n").toLatin1());
    }
    p.closeWriteChannel();

    if(!p.waitForFinished()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }
}

void runStyleMoc(const QString& header, const QStringList& arguments)
{
    QFile inf(header);
    if(!inf.open(QFile::ReadOnly))
        qFatal("Could not open header file for reading");

    QTextStream in(&inf);

    QProcess p;
    p.setProcessChannelMode( QProcess::ForwardedChannels );
    p.start(QString(QT_MOC_PATH), arguments);
    if(!p.waitForStarted()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }


    QRegExp attributeRegExp("\\s*M_STYLE_ATTRIBUTE\\s*\\(\\s*(\\w+\\:*\\w*)\\s*,\\s*(\\w+)\\s*,\\s*(\\w+)\\s*\\)\\s*");
    QRegExp attributePtrRegExp("\\s*M_STYLE_PTR_ATTRIBUTE\\s*\\(\\s*(\\w+\\:*\\w*\\s*\\*+)\\s*,\\s*(\\w+)\\s*,\\s*(\\w+)\\s*\\)\\s*");


    QString line;
    while(true) {
        line = in.readLine();
        if(line.isNull()) {
            break;
        }

        line.replace(attributeRegExp, "    Q_PROPERTY(\\1 \\2 READ \\2 WRITE set\\3)");
        line.replace(attributePtrRegExp, "    Q_PROPERTY(const \\1 \\2 READ \\2 WRITE set\\3)");
        p.write(QString(line + "\n").toLatin1());
    }
    p.closeWriteChannel();

    if(!p.waitForFinished()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }
}

void runQtMoc(const QStringList& arguments)
{
    QProcess p;
    p.setProcessChannelMode( QProcess::ForwardedChannels );
    p.start(QString(QT_MOC_PATH), arguments);

    if(!p.waitForStarted()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }

    if(!p.waitForFinished()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }
}

enum HeaderType {
    Model,
    Style
};

int main(int argc, const char *argv[])
{
    HeaderType type=Model;
    QStringList commandLineParameters;
    QString filename;

    for(int i=1; i<argc; ++i) {<commit_msg>Changes: fix mmoc handling of qt moc exit codes<commit_after>#include <QtGlobal>
#include <QFileInfo>
#include <QDir>
#include <QProcess>
#include <QtDebug>


void runModelMoc(const QString& header, const QStringList& arguments)
{
    QFile inf(header);
    if(!inf.open(QFile::ReadOnly))
        qFatal("Could not open header file for reading");

    QTextStream in(&inf);

    QProcess p;
    p.setProcessChannelMode( QProcess::ForwardedChannels );
    p.start(QString(QT_MOC_PATH), arguments, QIODevice::WriteOnly);
    if(!p.waitForStarted()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }

    QString spaces = "\\s*";
    QString comma = ",";
    QString colon = ":";
    QString parenO = "\\(";
    QString parenC = "\\)";
    QString emptyParen = "\\(\\)";
    QString angleO = "<";
    QString angleC = ">";
    QString braceO = "\\{";
    QString nameSpace = "(?:\\w+::)";
    QString typeName = "\\w+";
    QString pointer = "\\*";
    QString templateName = "\\w+";
    QString plainParam = "(\\w+)";
    QString boolParam = "(true|false)";
    QString anyParam = "(.+)";

    QRegExp propertyRegExp(
            spaces+
            "M_MODEL_PROPERTY"+
            spaces+parenO+spaces+
                "("+
                    "(?:"+
                         nameSpace+"{,1}"+
                         typeName+
                         spaces+
                         pointer+"{,1}"+
                    ")"+
                    "|"+
                    "(?:"+
                         templateName+
                         angleO+
                             spaces+
                             typeName+
                             spaces+
                             pointer+"{,1}"+
                             spaces+
                         angleC+
                     ")"+
                  ")"+
                      spaces+comma+spaces+
                  plainParam+
                      spaces+comma+spaces+
                  plainParam+
                      spaces+comma+spaces+
                  plainParam+
                      spaces+comma+spaces+
                  anyParam+
              spaces+parenC+spaces );

    QRegExp propertyPtrRegExp(
        spaces+
        "M_MODEL_PTR_PROPERTY"+
        spaces+parenO+spaces+
            "("+
                "(?:"+
                    nameSpace+"{,1}"+
                    typeName+
                    spaces+
                    pointer+"{,1}"+
                    spaces+
                ")"+
                "|"+
                "(?:"+
                    templateName+
                    angleO+
                        spaces+
                        typeName+
                        spaces+
                        pointer+"{,1}"+
                        spaces+
                    angleC+
                ")"+
            ")"+
                spaces+comma+spaces+
            plainParam+
                spaces+comma+spaces+
            plainParam+
                spaces+comma+spaces+
            boolParam+
                spaces+comma+spaces+
            anyParam+
        spaces+parenC+spaces );

    QString line;
    while(true) {
        line = in.readLine();
        if(line.isNull()) {
            break;
        }

        line.replace(propertyRegExp, "    Q_PROPERTY(\\1 \\2 READ \\2 WRITE set\\3)");
        line.replace(propertyPtrRegExp, "    Q_PROPERTY(\\1 \\2 READ \\2 WRITE set\\3)");
        p.write(QString(line + "\n").toLatin1());
    }
    p.closeWriteChannel();

    if(!p.waitForFinished()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }

    if ( p.exitCode() != 0 ) {
        qFatal("mmoc: qt moc ended with error: %i", p.exitCode() );
    }
}

void runStyleMoc(const QString& header, const QStringList& arguments)
{
    QFile inf(header);
    if(!inf.open(QFile::ReadOnly))
        qFatal("Could not open header file for reading");

    QTextStream in(&inf);

    QProcess p;
    p.setProcessChannelMode( QProcess::ForwardedChannels );
    p.start(QString(QT_MOC_PATH), arguments);
    if(!p.waitForStarted()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }


    QRegExp attributeRegExp("\\s*M_STYLE_ATTRIBUTE\\s*\\(\\s*(\\w+\\:*\\w*)\\s*,\\s*(\\w+)\\s*,\\s*(\\w+)\\s*\\)\\s*");
    QRegExp attributePtrRegExp("\\s*M_STYLE_PTR_ATTRIBUTE\\s*\\(\\s*(\\w+\\:*\\w*\\s*\\*+)\\s*,\\s*(\\w+)\\s*,\\s*(\\w+)\\s*\\)\\s*");


    QString line;
    while(true) {
        line = in.readLine();
        if(line.isNull()) {
            break;
        }

        line.replace(attributeRegExp, "    Q_PROPERTY(\\1 \\2 READ \\2 WRITE set\\3)");
        line.replace(attributePtrRegExp, "    Q_PROPERTY(const \\1 \\2 READ \\2 WRITE set\\3)");
        p.write(QString(line + "\n").toLatin1());
    }
    p.closeWriteChannel();

    if(!p.waitForFinished()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }

    if ( p.exitCode() != 0 ) {
        qFatal("mmoc: qt moc ended with error: %i", p.exitCode() );
    }
}

void runQtMoc(const QStringList& arguments)
{
    QProcess p;
    p.setProcessChannelMode( QProcess::ForwardedChannels );
    p.start(QString(QT_MOC_PATH), arguments);

    if(!p.waitForStarted()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }

    if(!p.waitForFinished()) {
        qFatal("mmoc: failed to run command '%s'", QT_MOC_PATH);
    }

    if ( p.exitCode() != 0 ) {
        qFatal("mmoc: qt moc ended with error: %i", p.exitCode() );
    }
}

enum HeaderType {
    Model,
    Style
};

int main(int argc, const char *argv[])
{
    HeaderType type=Model;
    QStringList commandLineParameters;
    QString filename;

    for(int i=1; i<argc; ++i) {
{'fraction_non_alphanumeric': 0.1539573545355981, 'fraction_numerical': 0.004788579689194073, 'mean_word_length': 1.4768404564779594, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 15, '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': '1916717', 'n_tokens_mistral': 3511, 'n_tokens_neox': 3363, 'n_words': 607}