diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-15 13:33:04 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-15 15:57:07 +0200 |
commit | 3030f9c99e82c4ea0cc65d6573e90b7c80fe419f (patch) | |
tree | 8166e6ed0435dfd026c3766050d6b2662b5d5fe2 | |
parent | d0678ae7cac8f80a5b80042edb13335f438bb9ce (diff) |
shiboken6: Add a dot view
shiboken uses directed graphs in a few places (class dependencies,
overload order). Add a helper function to show the graphs
for debugging purposes.
Task-number: PYSIDE-1660
Change-Id: Ie760954ddf8cb7aa31db2c79854063159658ed4b
Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r-- | sources/shiboken6/ApiExtractor/CMakeLists.txt | 1 | ||||
-rw-r--r-- | sources/shiboken6/ApiExtractor/dotview.cpp | 81 | ||||
-rw-r--r-- | sources/shiboken6/ApiExtractor/dotview.h | 39 | ||||
-rw-r--r-- | sources/shiboken6/ApiExtractor/graph.h | 26 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/overloaddata.cpp | 6 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/overloaddata.h | 1 |
6 files changed, 153 insertions, 1 deletions
diff --git a/sources/shiboken6/ApiExtractor/CMakeLists.txt b/sources/shiboken6/ApiExtractor/CMakeLists.txt index 040d85f54..8d2d194ab 100644 --- a/sources/shiboken6/ApiExtractor/CMakeLists.txt +++ b/sources/shiboken6/ApiExtractor/CMakeLists.txt @@ -18,6 +18,7 @@ abstractmetalang.cpp codesniphelpers.cpp conditionalstreamreader.cpp documentation.cpp +dotview.cpp enclosingclassmixin.cpp fileout.cpp messages.cpp diff --git a/sources/shiboken6/ApiExtractor/dotview.cpp b/sources/shiboken6/ApiExtractor/dotview.cpp new file mode 100644 index 000000000..0ead28f68 --- /dev/null +++ b/sources/shiboken6/ApiExtractor/dotview.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt for Python. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** 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. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dotview.h" + +#include <QtCore/QDebug> +#include <QtCore/QDir> +#include <QtCore/QFile> +#include <QtCore/QProcess> +#include <QtCore/QTemporaryFile> + +bool showDotGraph(const QString &name, const QString &graph) +{ + const QString imageType = u"jpg"_qs; + + // Write out the graph to a temporary file + QTemporaryFile dotFile(QDir::tempPath() + u'/' + name + u"_XXXXXX.dot"_qs); + if (!dotFile.open()) { + qWarning("Cannot open temporary file: %s", qPrintable(dotFile.errorString())); + return false; + } + const QString tempDotFile = dotFile.fileName(); + dotFile.write(graph.toUtf8()); + dotFile.close(); + + // Convert to image using "dot" + const QString imageFile = tempDotFile.left(tempDotFile.size() - 3) + imageType; + QProcess process; + process.start(u"dot"_qs, {u"-T"_qs + imageType, u"-o"_qs + imageFile, tempDotFile}); + if (!process.waitForStarted() || !process.waitForFinished()) { + qWarning("Image conversion failed: %s", qPrintable(process.errorString())); + return false; + } + if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) { + qWarning("Image conversion failed (%d): %s", + process.exitCode(), + process.readAllStandardError().constData()); + return false; + } + + // Launch image. Should use QDesktopServices::openUrl(), + // but we don't link against QtGui +#ifdef Q_OS_UNIX + const QString imageViewer = u"gwenview"_qs; +#else + const QString imageViewer = u"mspaint"_qs; +#endif + if (!QProcess::startDetached(imageViewer, {imageFile})) { + qWarning("Failed to launch viewer: %s", qPrintable(imageViewer)); + return false; + } + qInfo().noquote().nospace() << "Viewing: " + << QDir::toNativeSeparators(tempDotFile) + << ' ' << QDir::toNativeSeparators(imageFile); + return true; +} diff --git a/sources/shiboken6/ApiExtractor/dotview.h b/sources/shiboken6/ApiExtractor/dotview.h new file mode 100644 index 000000000..462cc2b75 --- /dev/null +++ b/sources/shiboken6/ApiExtractor/dotview.h @@ -0,0 +1,39 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt for Python. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** 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. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DOTVIEW_H +#define DOTVIEW_H + +#include <QtCore/QString> + +/// Show a dot digraph in an image viewer +/// \param name base name for files +/// \param graph graph +bool showDotGraph(const QString &name, const QString &graph); + +#endif // DOTVIEW_H diff --git a/sources/shiboken6/ApiExtractor/graph.h b/sources/shiboken6/ApiExtractor/graph.h index 0961f85af..26aed4328 100644 --- a/sources/shiboken6/ApiExtractor/graph.h +++ b/sources/shiboken6/ApiExtractor/graph.h @@ -29,6 +29,8 @@ #ifndef GRAPH_H #define GRAPH_H +#include "dotview.h" + #include <QtCore/QDebug> #include <QtCore/QFile> #include <QtCore/QHash> @@ -98,6 +100,10 @@ public: /// \param f function returning the name of a node template <class NameFunction> bool dumpDot(const QString& fileName, NameFunction f) const; + template <class NameFunction> + void formatDot(QTextStream &str, NameFunction f) const; + template <class NameFunction> + bool showGraph(const QString &name, NameFunction f) const; void format(QDebug &debug) const; @@ -251,6 +257,15 @@ bool Graph<Node>::dumpDot(const QString& fileName, if (!output.open(QIODevice::WriteOnly)) return false; QTextStream s(&output); + formatDot(s, nameFunction); + return true; +} + +template <class Node> +template <class NameFunction> +void Graph<Node>::formatDot(QTextStream &s, + NameFunction nameFunction) const +{ s << "digraph D {\n"; for (const auto &nodeEntry : m_nodeEntries) { if (!nodeEntry.targets.isEmpty()) { @@ -260,7 +275,16 @@ bool Graph<Node>::dumpDot(const QString& fileName, } } s << "}\n"; - return true; +} + +template <class Node> +template <class NameFunction> +bool Graph<Node>::showGraph(const QString &name, NameFunction f) const +{ + QString graph; + QTextStream s(&graph); + formatDot(s, f); + return showDotGraph(name, graph); } template <class Node> diff --git a/sources/shiboken6/generator/shiboken/overloaddata.cpp b/sources/shiboken6/generator/shiboken/overloaddata.cpp index 137a37aeb..a359e8024 100644 --- a/sources/shiboken6/generator/shiboken/overloaddata.cpp +++ b/sources/shiboken6/generator/shiboken/overloaddata.cpp @@ -29,6 +29,7 @@ #include <abstractmetafunction.h> #include <apiextractorresult.h> #include <abstractmetalang.h> +#include <dotview.h> #include <reporthandler.h> #include <typesystem.h> #include <graph.h> @@ -787,6 +788,11 @@ QString OverloadData::dumpGraph() const return result; } +bool OverloadData::showGraph() const +{ + return showDotGraph(referenceFunction()->name(), dumpGraph()); +} + static inline QString toHtml(QString s) { s.replace(QLatin1Char('<'), QLatin1String("<")); diff --git a/sources/shiboken6/generator/shiboken/overloaddata.h b/sources/shiboken6/generator/shiboken/overloaddata.h index fb367b259..0271fd3d0 100644 --- a/sources/shiboken6/generator/shiboken/overloaddata.h +++ b/sources/shiboken6/generator/shiboken/overloaddata.h @@ -179,6 +179,7 @@ public: void dumpGraph(const QString &filename) const; QString dumpGraph() const; + bool showGraph() const; /// Returns true if a list of arguments is used (METH_VARARGS) bool pythonFunctionWrapperUsesListOfArguments() const; |