diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-07-20 11:42:02 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-07-23 10:16:02 +0000 |
commit | 047a658aac4c01d0ec3deb0459a1a88328ed9821 (patch) | |
tree | f10c1f88233af4e79fef1a00995ab9ce1ddcd8e4 | |
parent | e36aedaf2182cf81c919c5825c694c6ae3908198 (diff) |
shiboken: Add options for diff and dry-run
Refactor and breathe life back into the existing diff/dry-run
functionality of class FileOut and make it available via command line
options.
Change-Id: I32e1e262c0c2e8f107776923477fe9714ad295cf
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
-rw-r--r-- | sources/shiboken2/ApiExtractor/fileout.cpp | 151 | ||||
-rw-r--r-- | sources/shiboken2/ApiExtractor/fileout.h | 8 | ||||
-rw-r--r-- | sources/shiboken2/doc/commandlineoptions.rst | 10 | ||||
-rw-r--r-- | sources/shiboken2/generator/main.cpp | 20 |
4 files changed, 106 insertions, 83 deletions
diff --git a/sources/shiboken2/ApiExtractor/fileout.cpp b/sources/shiboken2/ApiExtractor/fileout.cpp index e3c96d57b..522c98fb5 100644 --- a/sources/shiboken2/ApiExtractor/fileout.cpp +++ b/sources/shiboken2/ApiExtractor/fileout.cpp @@ -50,24 +50,25 @@ static const char colorInfo[] = ""; static const char colorReset[] = ""; #endif -FileOut::FileOut(QString n): - name(n), - stream(&tmp), - isDone(false) -{} +FileOut::FileOut(QString n) : + name(std::move(n)), + stream(&tmp), + isDone(false) +{ +} + +FileOut::~FileOut() +{ + if (!isDone) + done(); +} -static int* lcsLength(QList<QByteArray> a, QList<QByteArray> b) +static QVector<int> lcsLength(const QByteArrayList &a, const QByteArrayList &b) { const int height = a.size() + 1; const int width = b.size() + 1; - int *res = new int[width * height]; - - for (int row = 0; row < height; row++) - res[width * row] = 0; - - for (int col = 0; col < width; col++) - res[col] = 0; + QVector<int> res(width * height, 0); for (int row = 1; row < height; row++) { for (int col = 1; col < width; col++) { @@ -89,88 +90,84 @@ enum Type { struct Unit { - Unit(Type type, int pos) : - type(type), - start(pos), - end(pos) {} - Type type; int start; int end; - void print(QList<QByteArray> a, QList<QByteArray> b) - { - if (type == Unchanged) { - if ((end - start) > 9) { - for (int i = start; i <= start + 2; i++) - std::printf(" %s\n", a[i].data()); - std::printf("%s=\n= %d more lines\n=%s\n", colorInfo, end - start - 6, colorReset); - for (int i = end - 2; i <= end; i++) - std::printf(" %s\n", a[i].data()); - } else { - for (int i = start; i <= end; i++) - std::printf(" %s\n", a[i].data()); - } - } else if (type == Add) { - std::printf("%s", colorAdd); - for (int i = start; i <= end; i++) - std::printf("+ %s\n", b[i].data()); - std::printf("%s", colorReset); - } else if (type == Delete) { - std::printf("%s", colorDelete); - for (int i = start; i <= end; i++) - std::printf("- %s\n", a[i].data()); - std::printf("%s", colorReset); - } - } + void print(const QByteArrayList &a, const QByteArrayList &b) const; }; -static QList<Unit*> *unitAppend(QList<Unit*> *res, Type type, int pos) +void Unit::print(const QByteArrayList &a, const QByteArrayList &b) const { - if (!res) { - res = new QList<Unit*>; - res->append(new Unit(type, pos)); - return res; + switch (type) { + case Unchanged: + if ((end - start) > 9) { + for (int i = start; i <= start + 2; i++) + std::printf(" %s\n", a.at(i).constData()); + std::printf("%s=\n= %d more lines\n=%s\n", + colorInfo, end - start - 6, colorReset); + for (int i = end - 2; i <= end; i++) + std::printf(" %s\n", a.at(i).constData()); + } else { + for (int i = start; i <= end; i++) + std::printf(" %s\n", a.at(i).constData()); + } + break; + case Add: + std::fputs(colorAdd, stdout); + for (int i = start; i <= end; i++) + std::printf("+ %s\n", b.at(i).constData()); + std::fputs(colorReset, stdout); + break; + case Delete: + std::fputs(colorDelete, stdout); + for (int i = start; i <= end; i++) + std::printf("- %s\n", a.at(i).constData()); + std::fputs(colorReset, stdout); + break; } +} - Unit *last = res->last(); - if (last->type == type) - last->end = pos; +static void unitAppend(Type type, int pos, QVector<Unit> *units) +{ + if (!units->isEmpty() && units->last().type == type) + units->last().end = pos; else - res->append(new Unit(type, pos)); - - return res; + units->append(Unit{type, pos, pos}); } -static QList<Unit*> *diffHelper(int *lcs, QList<QByteArray> a, QList<QByteArray> b, int row, int col) +static QVector<Unit> diffHelper(const QVector<int> &lcs, + const QByteArrayList &a, const QByteArrayList &b, + int row, int col) { - if (row > 0 && col > 0 && (a[row-1] == b[col-1])) { - return unitAppend(diffHelper(lcs, a, b, row - 1, col - 1), Unchanged, row - 1); - } else { - int width = b.size() + 1; - if ((col > 0) - && (row == 0 || lcs[width * row + col-1] >= lcs[width *(row-1) + col])) { - return unitAppend(diffHelper(lcs, a, b, row, col - 1), Add, col - 1); - } else if ((row > 0) - && (col == 0 || lcs[width * row + col-1] < lcs[width *(row-1) + col])) { - return unitAppend(diffHelper(lcs, a, b, row - 1, col), Delete, row - 1); - } + if (row > 0 && col > 0 && a.at(row - 1) == b.at(col - 1)) { + QVector<Unit> result = diffHelper(lcs, a, b, row - 1, col - 1); + unitAppend(Unchanged, row - 1, &result); + return result; } - delete lcs; - return 0; -} -static void diff(QList<QByteArray> a, QList<QByteArray> b) -{ - QList<Unit*> *res = diffHelper(lcsLength(a, b), a, b, a.size(), b.size()); - for (int i = 0; i < res->size(); i++) { - Unit *unit = res->at(i); - unit->print(a, b); - delete(unit); + const int width = b.size() + 1; + if (col > 0 + && (row == 0 || lcs.at(width * row + col -1 ) >= lcs.at(width * (row - 1) + col))) { + QVector<Unit> result = diffHelper(lcs, a, b, row, col - 1); + unitAppend(Add, col - 1, &result); + return result; + } + if (row > 0 + && (col == 0 || lcs.at(width * row + col-1) < lcs.at(width * (row - 1) + col))) { + QVector<Unit> result = diffHelper(lcs, a, b, row - 1, col); + unitAppend(Delete, row - 1, &result); + return result; } - delete(res); + return QVector<Unit>{}; } +static void diff(const QByteArrayList &a, const QByteArrayList &b) +{ + const QVector<Unit> res = diffHelper(lcsLength(a, b), a, b, a.size(), b.size()); + for (const Unit &unit : res) + unit.print(a, b); +} FileOut::State FileOut::done() { diff --git a/sources/shiboken2/ApiExtractor/fileout.h b/sources/shiboken2/ApiExtractor/fileout.h index 539ae7a43..ed995425d 100644 --- a/sources/shiboken2/ApiExtractor/fileout.h +++ b/sources/shiboken2/ApiExtractor/fileout.h @@ -43,12 +43,8 @@ private: public: enum State { Failure, Unchanged, Success }; - FileOut(QString name); - ~FileOut() - { - if (!isDone) - done(); - } + explicit FileOut(QString name); + ~FileOut(); State done(); State done(QString *errorMessage); diff --git a/sources/shiboken2/doc/commandlineoptions.rst b/sources/shiboken2/doc/commandlineoptions.rst index 7a632f4c4..c335fab75 100644 --- a/sources/shiboken2/doc/commandlineoptions.rst +++ b/sources/shiboken2/doc/commandlineoptions.rst @@ -69,6 +69,16 @@ Options ``--generation-set`` Generator set to be used (e.g. qtdoc). +.. _diff: + +``--diff`` + Print a diff of wrapper files. + +.. _dryrun: + +``--dryrun`` + Dry run, do not generate wrapper files. + .. _--project-file: ``--project-file=<file>`` diff --git a/sources/shiboken2/generator/main.cpp b/sources/shiboken2/generator/main.cpp index 900f69166..67d838ce8 100644 --- a/sources/shiboken2/generator/main.cpp +++ b/sources/shiboken2/generator/main.cpp @@ -34,6 +34,7 @@ #include <QtCore/QDir> #include <iostream> #include <apiextractor.h> +#include <fileout.h> #include "generator.h" #include "shibokenconfig.h" #include "cppgenerator.h" @@ -52,6 +53,9 @@ static inline QString frameworkIncludePathOption() { return QStringLiteral("fram static inline QString systemIncludePathOption() { return QStringLiteral("system-include-paths"); } static inline QString typesystemPathOption() { return QStringLiteral("typesystem-paths"); } static inline QString helpOption() { return QStringLiteral("help"); } +static inline QString diffOption() { return QStringLiteral("diff"); } +static inline QString dryrunOption() { return QStringLiteral("dry-run"); } + static const char helpHint[] = "Note: use --help or -h for more information.\n"; typedef QMap<QString, QString> CommandArgumentMap; @@ -316,6 +320,10 @@ void printUsage() QLatin1String("System include paths used by the C++ parser")) << qMakePair(QLatin1String("generator-set=<\"generator module\">"), QLatin1String("generator-set to be used. e.g. qtdoc")) + << qMakePair(diffOption(), + QLatin1String("Print a diff of wrapper files")) + << qMakePair(dryrunOption(), + QLatin1String("Dry run, do not generate wrapper files")) << qMakePair(QLatin1String("-h"), QString()) << qMakePair(helpOption(), QLatin1String("Display this help and exit")) @@ -437,6 +445,18 @@ int main(int argc, char *argv[]) return EXIT_SUCCESS; } + ait = args.find(diffOption()); + if (ait != args.end()) { + args.erase(ait); + FileOut::diff = true; + } + + ait = args.find(dryrunOption()); + if (ait != args.end()) { + args.erase(ait); + FileOut::dummy = true; + } + QString licenseComment; ait = args.find(QLatin1String("license-file")); if (ait != args.end()) { |