diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-09-11 14:12:46 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-09-20 12:44:59 +0200 |
commit | b550babdf405dc513199713384a010b66126ebd6 (patch) | |
tree | 29a7791f929d67c71605f6379182832dc69011e2 | |
parent | 0730fdaa0e3eaadcbed9ba40080b617d2edb092c (diff) |
shiboken6: Make generator options statically accessible
Create struct withs options (to be used by an improved command line
parser later on) and make them static members of the generators.
This is a first step to remove the currently duplicated handling of
options since ShibokenGenerator is instantiated for HeaderGenerator
and CppGenerator.
Also, more generator functions can then be made statically accessible.
Change-Id: I1e355370ef32d35ddd9f1e7d847a1965eb44e077
Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Reviewed-by: Adrian Herrmann <adrian.herrmann@qt.io>
(cherry picked from commit e237c2e1a134207cdf34f31566b19669dd501cd9)
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r-- | sources/shiboken6/generator/generator.cpp | 24 | ||||
-rw-r--r-- | sources/shiboken6/generator/generator.h | 4 | ||||
-rw-r--r-- | sources/shiboken6/generator/qtdoc/qtdocgenerator.cpp | 85 | ||||
-rw-r--r-- | sources/shiboken6/generator/qtdoc/qtdocgenerator.h | 7 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/shibokengenerator.cpp | 67 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/shibokengenerator.h | 28 |
6 files changed, 125 insertions, 90 deletions
diff --git a/sources/shiboken6/generator/generator.cpp b/sources/shiboken6/generator/generator.cpp index e34ff10e1..a0b36b509 100644 --- a/sources/shiboken6/generator/generator.cpp +++ b/sources/shiboken6/generator/generator.cpp @@ -32,6 +32,12 @@ using namespace Qt::StringLiterals; static const char ENABLE_PYSIDE_EXTENSIONS[] = "enable-pyside-extensions"; static const char AVOID_PROTECTED_HACK[] = "avoid-protected-hack"; +struct GeneratorOptions +{ + bool usePySideExtensions = false; + bool avoidProtectedHack = false; +}; + struct Generator::GeneratorPrivate { ApiExtractorResult api; @@ -40,10 +46,11 @@ struct Generator::GeneratorPrivate QString licenseComment; AbstractMetaClassCList m_invisibleTopNamespaces; bool m_hasPrivateClasses = false; - bool m_usePySideExtensions = false; - bool m_avoidProtectedHack = false; + static GeneratorOptions m_options; }; +GeneratorOptions Generator::GeneratorPrivate::m_options; + Generator::Generator() : m_d(new GeneratorPrivate) { } @@ -91,10 +98,11 @@ Generator::OptionDescriptions Generator::options() const bool Generator::handleOption(const QString & key, const QString & /* value */) { + auto &options = GeneratorPrivate::m_options; if (key == QLatin1StringView(ENABLE_PYSIDE_EXTENSIONS)) - return ( m_d->m_usePySideExtensions = true); + return ( options.usePySideExtensions = true); if (key == QLatin1StringView(AVOID_PROTECTED_HACK)) - return (m_d->m_avoidProtectedHack = true); + return ( options.avoidProtectedHack = true); return false; } @@ -267,14 +275,14 @@ bool Generator::hasPrivateClasses() const return m_d->m_hasPrivateClasses; } -bool Generator::usePySideExtensions() const +bool Generator::usePySideExtensions() { - return m_d->m_usePySideExtensions; + return GeneratorPrivate::m_options.usePySideExtensions; } -bool Generator::avoidProtectedHack() const +bool Generator::avoidProtectedHack() { - return m_d->m_avoidProtectedHack; + return GeneratorPrivate::m_options.avoidProtectedHack; } QString Generator::getFullTypeName(TypeEntryCPtr type) diff --git a/sources/shiboken6/generator/generator.h b/sources/shiboken6/generator/generator.h index 9d87ada6b..b383a145d 100644 --- a/sources/shiboken6/generator/generator.h +++ b/sources/shiboken6/generator/generator.h @@ -94,10 +94,10 @@ public: bool hasPrivateClasses() const; /// Returns true if the user enabled PySide extensions (command line option) - bool usePySideExtensions() const; + static bool usePySideExtensions(); /// Returns true if the generated code should not use the /// "#define protected public" hack. - bool avoidProtectedHack() const; + static bool avoidProtectedHack(); /** * Retrieves the name of the currently processed module. diff --git a/sources/shiboken6/generator/qtdoc/qtdocgenerator.cpp b/sources/shiboken6/generator/qtdoc/qtdocgenerator.cpp index 6d29ceec8..0d77b7440 100644 --- a/sources/shiboken6/generator/qtdoc/qtdocgenerator.cpp +++ b/sources/shiboken6/generator/qtdoc/qtdocgenerator.cpp @@ -42,6 +42,15 @@ using namespace Qt::StringLiterals; +struct DocGeneratorOptions +{ + QtXmlToSphinxParameters parameters; + QString extraSectionDir; + QString additionalDocumentationList; + QString inheritanceFile; + bool doxygen = false; +}; + struct GeneratorDocumentation { struct Property @@ -205,9 +214,11 @@ struct propRef : public shortDocRef // Attribute/property (short) reference shortDocRef("attr", target) {} }; +DocGeneratorOptions QtDocGenerator::m_options; + QtDocGenerator::QtDocGenerator() { - m_parameters.snippetComparison = + m_options.parameters.snippetComparison = ReportHandler::debugLevel() >= ReportHandler::FullDebug; } @@ -253,7 +264,7 @@ void QtDocGenerator::writeFormattedText(TextStream &s, const QString &doc, metaClassName = metaClass->fullName(); if (format == Documentation::Native) { - QtXmlToSphinx x(this, m_parameters, doc, metaClassName); + QtXmlToSphinx x(this, m_options.parameters, doc, metaClassName); s << x; } else { const auto lines = QStringView{doc}.split(u'\n'); @@ -867,18 +878,18 @@ bool QtDocGenerator::finishGeneration() { if (!api().classes().isEmpty()) writeModuleDocumentation(); - if (!m_additionalDocumentationList.isEmpty()) + if (!m_options.additionalDocumentationList.isEmpty()) writeAdditionalDocumentation(); - if (!m_inheritanceFile.isEmpty() && !writeInheritanceFile()) + if (!m_options.inheritanceFile.isEmpty() && !writeInheritanceFile()) return false; return true; } bool QtDocGenerator::writeInheritanceFile() { - QFile inheritanceFile(m_inheritanceFile); + QFile inheritanceFile(m_options.inheritanceFile); if (!inheritanceFile.open(QIODevice::WriteOnly | QIODevice::Text)) - throw Exception(msgCannotOpenForWriting(m_inheritanceFile)); + throw Exception(msgCannotOpenForWriting(m_options.inheritanceFile)); QJsonObject dict; for (const auto &c : api().classes()) { @@ -922,11 +933,11 @@ void QtDocGenerator::writeModuleDocumentation() moduleName.remove(0, lastIndex + 1); // Search for extra-sections - if (!m_extraSectionDir.isEmpty()) { - QDir extraSectionDir(m_extraSectionDir); + if (!m_options.extraSectionDir.isEmpty()) { + QDir extraSectionDir(m_options.extraSectionDir); if (!extraSectionDir.exists()) { - const QString m = QStringLiteral("Extra sections directory ") + - m_extraSectionDir + QStringLiteral(" doesn't exist"); + const QString m = u"Extra sections directory "_s + + m_options.extraSectionDir + u" doesn't exist"_s; throw Exception(m); } @@ -958,7 +969,7 @@ void QtDocGenerator::writeModuleDocumentation() << "Detailed Description\n--------------------\n\n"; // module doc is always wrong and C++istic, so go straight to the extra directory! - QFile moduleDoc(m_extraSectionDir + u'/' + moduleName + QFile moduleDoc(m_options.extraSectionDir + u'/' + moduleName + u".rst"_s); if (moduleDoc.open(QIODevice::ReadOnly | QIODevice::Text)) { s << moduleDoc.readAll(); @@ -969,7 +980,7 @@ void QtDocGenerator::writeModuleDocumentation() if (moduleDoc.format() == Documentation::Native) { QString context = it.key(); QtXmlToSphinx::stripPythonQualifiers(&context); - QtXmlToSphinx x(this, m_parameters, moduleDoc.detailed(), context); + QtXmlToSphinx x(this, m_options.parameters, moduleDoc.detailed(), context); s << x; } else { s << moduleDoc.detailed(); @@ -996,7 +1007,7 @@ static inline QString msgNonExistentAdditionalDocFile(const QString &dir, void QtDocGenerator::writeAdditionalDocumentation() const { - QFile additionalDocumentationFile(m_additionalDocumentationList); + QFile additionalDocumentationFile(m_options.additionalDocumentationList); if (!additionalDocumentationFile.open(QIODevice::ReadOnly | QIODevice::Text)) throw Exception(msgCannotOpenForReading(additionalDocumentationFile)); @@ -1030,7 +1041,7 @@ void QtDocGenerator::writeAdditionalDocumentation() const } } else { // Normal file entry - QFileInfo fi(m_parameters.docDataDir + u'/' + line); + QFileInfo fi(m_options.parameters.docDataDir + u'/' + line); if (fi.isFile()) { const QString rstFileName = fi.baseName() + rstSuffix; const QString rstFile = targetDir + u'/' + rstFileName; @@ -1048,7 +1059,7 @@ void QtDocGenerator::writeAdditionalDocumentation() const // FIXME: This should be an exception, in principle, but it // requires building all modules. qCWarning(lcShibokenDoc, "%s", - qPrintable(msgNonExistentAdditionalDocFile(m_parameters.docDataDir, line))); + qPrintable(msgNonExistentAdditionalDocFile(m_options.parameters.docDataDir, line))); } ++count; } @@ -1067,24 +1078,28 @@ void QtDocGenerator::writeAdditionalDocumentation() const bool QtDocGenerator::doSetup() { - if (m_parameters.codeSnippetDirs.isEmpty()) { - m_parameters.codeSnippetDirs = - m_parameters.libSourceDir.split(QLatin1Char(PATH_SEP)); + if (m_options.parameters.codeSnippetDirs.isEmpty()) { + m_options.parameters.codeSnippetDirs = + m_options.parameters.libSourceDir.split(QLatin1Char(PATH_SEP)); } - if (m_docParser.isNull()) - m_docParser.reset(new QtDocParser); + if (m_docParser.isNull()) { + if (m_options.doxygen) + m_docParser.reset(new DoxygenParser); + else + m_docParser.reset(new QtDocParser); + } - if (m_parameters.libSourceDir.isEmpty() - || m_parameters.docDataDir.isEmpty()) { + if (m_options.parameters.libSourceDir.isEmpty() + || m_options.parameters.docDataDir.isEmpty()) { qCWarning(lcShibokenDoc) << "Documentation data dir and/or Qt source dir not informed, " "documentation will not be extracted from Qt sources."; return false; } - m_docParser->setDocumentationDataDirectory(m_parameters.docDataDir); - m_docParser->setLibrarySourceDirectory(m_parameters.libSourceDir); - m_parameters.outputDirectory = outputDirectory(); + m_docParser->setDocumentationDataDirectory(m_options.parameters.docDataDir); + m_docParser->setLibrarySourceDirectory(m_options.parameters.libSourceDir); + m_options.parameters.outputDirectory = outputDirectory(); return true; } @@ -1121,15 +1136,15 @@ bool QtDocGenerator::handleOption(const QString &key, const QString &value) if (Generator::handleOption(key, value)) return true; if (key == u"library-source-dir") { - m_parameters.libSourceDir = value; + m_options.parameters.libSourceDir = value; return true; } if (key == u"documentation-data-dir") { - m_parameters.docDataDir = value; + m_options.parameters.docDataDir = value; return true; } if (key == u"documentation-code-snippets-dir") { - m_parameters.codeSnippetDirs = value.split(QLatin1Char(PATH_SEP)); + m_options.parameters.codeSnippetDirs = value.split(QLatin1Char(PATH_SEP)); return true; } @@ -1137,28 +1152,28 @@ bool QtDocGenerator::handleOption(const QString &key, const QString &value) const auto pos = value.indexOf(u':'); if (pos == -1) return false; - m_parameters.codeSnippetRewriteOld= value.left(pos); - m_parameters.codeSnippetRewriteNew = value.mid(pos + 1); + m_options.parameters.codeSnippetRewriteOld= value.left(pos); + m_options.parameters.codeSnippetRewriteNew = value.mid(pos + 1); return true; } if (key == u"documentation-extra-sections-dir") { - m_extraSectionDir = value; + m_options.extraSectionDir = value; return true; } if (key == u"doc-parser") { qCDebug(lcShibokenDoc).noquote().nospace() << "doc-parser: " << value; if (value == u"doxygen") - m_docParser.reset(new DoxygenParser); + m_options.doxygen = true; return true; } if (key == additionalDocumentationOption()) { - m_additionalDocumentationList = value; + m_options.additionalDocumentationList = value; return true; } if (key == u"inheritance-file") { - m_inheritanceFile = value; + m_options.inheritanceFile = value; return true; } @@ -1180,7 +1195,7 @@ bool QtDocGenerator::convertToRst(const QString &sourceFileName, sourceFile.close(); FileOut targetFile(targetFileName); - QtXmlToSphinx x(this, m_parameters, doc, context); + QtXmlToSphinx x(this, m_options.parameters, doc, context); targetFile.stream << x; targetFile.done(); return true; diff --git a/sources/shiboken6/generator/qtdoc/qtdocgenerator.h b/sources/shiboken6/generator/qtdoc/qtdocgenerator.h index 3ad78b590..f078a66f4 100644 --- a/sources/shiboken6/generator/qtdoc/qtdocgenerator.h +++ b/sources/shiboken6/generator/qtdoc/qtdocgenerator.h @@ -14,7 +14,7 @@ #include "qtxmltosphinxinterface.h" class DocParser; - +struct DocGeneratorOptions; struct GeneratorDocumentation; /** @@ -110,13 +110,10 @@ private: GeneratorDocumentation generatorDocumentation(const AbstractMetaClassCPtr &cppClass) const; - QString m_extraSectionDir; QStringList m_functionList; QMap<QString, QStringList> m_packages; QScopedPointer<DocParser> m_docParser; - QtXmlToSphinxParameters m_parameters; - QString m_additionalDocumentationList; - QString m_inheritanceFile; + static DocGeneratorOptions m_options; }; #endif // DOCGENERATOR_H diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp index f2d21c0af..2e927c73a 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp @@ -74,6 +74,20 @@ const QString BEGIN_ALLOW_THREADS = u"PyThreadState *_save = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS"_s; const QString END_ALLOW_THREADS = u"PyEval_RestoreThread(_save); // Py_END_ALLOW_THREADS"_s; +struct ShibokenGeneratorOptions +{ + bool useCtorHeuristic = false; + bool userReturnValueHeuristic = false; + bool verboseErrorMessagesDisabled = false; + bool useIsNullAsNbNonZero = false; + // FIXME PYSIDE 7 Flip m_leanHeaders default or remove? + bool leanHeaders = false; + bool useOperatorBoolAsNbNonZero = false; + // FIXME PYSIDE 7 Flip generateImplicitConversions default or remove? + bool generateImplicitConversions = true; + bool wrapperDiagnostics = false; +}; + struct GeneratorClassInfoCacheEntry { ShibokenGenerator::FunctionGroups functionGroups; @@ -105,6 +119,10 @@ const ShibokenGenerator::TypeSystemConverterRegExps & return result; } +// Options are static to avoid duplicated handling since ShibokenGenerator +// is instantiated for HeaderGenerator and CppGenerator. +ShibokenGeneratorOptions ShibokenGenerator::m_options; + ShibokenGenerator::ShibokenGenerator() = default; ShibokenGenerator::~ShibokenGenerator() = default; @@ -357,6 +375,11 @@ QString ShibokenGenerator::fullPythonFunctionName(const AbstractMetaFunctionCPtr return funcName; } +bool ShibokenGenerator::wrapperDiagnostics() +{ + return m_options.wrapperDiagnostics; +} + QString ShibokenGenerator::protectedEnumSurrogateName(const AbstractMetaEnum &metaEnum) { QString result = metaEnum.fullName(); @@ -2245,23 +2268,23 @@ bool ShibokenGenerator::handleOption(const QString &key, const QString &value) if (Generator::handleOption(key, value)) return true; if (key == QLatin1StringView(PARENT_CTOR_HEURISTIC)) - return (m_useCtorHeuristic = true); + return (m_options.useCtorHeuristic = true); if (key == QLatin1StringView(RETURN_VALUE_HEURISTIC)) - return (m_userReturnValueHeuristic = true); + return (m_options.userReturnValueHeuristic = true); if (key == QLatin1StringView(DISABLE_VERBOSE_ERROR_MESSAGES)) - return (m_verboseErrorMessagesDisabled = true); + return (m_options.verboseErrorMessagesDisabled = true); if (key == QLatin1StringView(USE_ISNULL_AS_NB_NONZERO)) - return (m_useIsNullAsNbNonZero = true); + return (m_options.useIsNullAsNbNonZero = true); if (key == QLatin1StringView(LEAN_HEADERS)) - return (m_leanHeaders= true); + return (m_options.leanHeaders= true); if (key == QLatin1StringView(USE_OPERATOR_BOOL_AS_NB_NONZERO)) - return (m_useOperatorBoolAsNbNonZero = true); + return (m_options.useOperatorBoolAsNbNonZero = true); if (key == QLatin1StringView(NO_IMPLICIT_CONVERSIONS)) { - m_generateImplicitConversions = false; + m_options.generateImplicitConversions = false; return true; } if (key == QLatin1StringView(WRAPPER_DIAGNOSTICS)) - return (m_wrapperDiagnostics = true); + return (m_options.wrapperDiagnostics = true); return false; } @@ -2270,34 +2293,34 @@ bool ShibokenGenerator::doSetup() return true; } -bool ShibokenGenerator::useCtorHeuristic() const +bool ShibokenGenerator::useCtorHeuristic() { - return m_useCtorHeuristic; + return m_options.useCtorHeuristic; } -bool ShibokenGenerator::useReturnValueHeuristic() const +bool ShibokenGenerator::useReturnValueHeuristic() { - return m_userReturnValueHeuristic; + return m_options.userReturnValueHeuristic; } -bool ShibokenGenerator::useIsNullAsNbNonZero() const +bool ShibokenGenerator::useIsNullAsNbNonZero() { - return m_useIsNullAsNbNonZero; + return m_options.useIsNullAsNbNonZero; } -bool ShibokenGenerator::leanHeaders() const +bool ShibokenGenerator::leanHeaders() { - return m_leanHeaders; + return m_options.leanHeaders; } -bool ShibokenGenerator::useOperatorBoolAsNbNonZero() const +bool ShibokenGenerator::useOperatorBoolAsNbNonZero() { - return m_useOperatorBoolAsNbNonZero; + return m_options.useOperatorBoolAsNbNonZero; } -bool ShibokenGenerator::generateImplicitConversions() const +bool ShibokenGenerator::generateImplicitConversions() { - return m_generateImplicitConversions; + return m_options.generateImplicitConversions; } QString ShibokenGenerator::moduleCppPrefix(const QString &moduleName) @@ -2385,9 +2408,9 @@ QString ShibokenGenerator::getTypeIndexVariableName(const AbstractMetaType &type return result; } -bool ShibokenGenerator::verboseErrorMessagesDisabled() const +bool ShibokenGenerator::verboseErrorMessagesDisabled() { - return m_verboseErrorMessagesDisabled; + return m_options.verboseErrorMessagesDisabled; } bool ShibokenGenerator::pythonFunctionWrapperUsesListOfArguments(const AbstractMetaFunctionCPtr &func) const diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.h b/sources/shiboken6/generator/shiboken/shibokengenerator.h index 492fb1910..7febd1c0e 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.h +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.h @@ -24,6 +24,7 @@ class OverloadData; class TargetToNativeConversion; struct GeneratorClassInfoCacheEntry; struct IncludeGroup; +struct ShibokenGeneratorOptions; QT_FORWARD_DECLARE_CLASS(TextStream) @@ -200,7 +201,7 @@ protected: static QString fullPythonFunctionName(const AbstractMetaFunctionCPtr &func, bool forceFunc); - bool wrapperDiagnostics() const { return m_wrapperDiagnostics; } + static bool wrapperDiagnostics(); static QString protectedEnumSurrogateName(const AbstractMetaEnum &metaEnum); @@ -286,17 +287,17 @@ protected: bool handleOption(const QString &key, const QString &value) override; /// Returns true if the user enabled the so called "parent constructor heuristic". - bool useCtorHeuristic() const; + static bool useCtorHeuristic(); /// Returns true if the user enabled the so called "return value heuristic". - bool useReturnValueHeuristic() const; + static bool useReturnValueHeuristic(); /// Returns true if the generator should use the result of isNull()const to compute boolean casts. - bool useIsNullAsNbNonZero() const; + static bool useIsNullAsNbNonZero(); /// Whether to generate lean module headers - bool leanHeaders() const; + static bool leanHeaders(); /// Returns true if the generator should use operator bool to compute boolean casts. - bool useOperatorBoolAsNbNonZero() const; + static bool useOperatorBoolAsNbNonZero(); /// Generate implicit conversions of function arguments - bool generateImplicitConversions() const; + static bool generateImplicitConversions(); static QString cppApiVariableName(const QString &moduleName = QString()); static QString pythonModuleObjectName(const QString &moduleName = QString()); static QString convertersVariableName(const QString &moduleName = QString()); @@ -309,7 +310,7 @@ protected: static QString getTypeIndexVariableName(const AbstractMetaType &type) ; /// Returns true if the user don't want verbose error messages on the generated bindings. - bool verboseErrorMessagesDisabled() const; + static bool verboseErrorMessagesDisabled(); void collectContainerTypesFromConverterMacros(const QString &code, bool toPythonMacro); @@ -454,16 +455,7 @@ private: void replaceTemplateVariables(QString &code, const AbstractMetaFunctionCPtr &func) const; - bool m_useCtorHeuristic = false; - bool m_userReturnValueHeuristic = false; - bool m_verboseErrorMessagesDisabled = false; - bool m_useIsNullAsNbNonZero = false; - // FIXME PYSIDE 7 Flip m_leanHeaders default or remove? - bool m_leanHeaders = false; - bool m_useOperatorBoolAsNbNonZero = false; - // FIXME PYSIDE 7 Flip generateImplicitConversions default or remove? - bool m_generateImplicitConversions = true; - bool m_wrapperDiagnostics = false; + static ShibokenGeneratorOptions m_options; /// Type system converter variable replacement names and regular expressions. static const QHash<int, QString> &typeSystemConvName(); |