diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2019-01-30 13:25:17 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2019-01-31 07:12:14 +0000 |
commit | d2ea4919081b5b61aa77029fb47e2cdf8e94adca (patch) | |
tree | 7dc16c12be158eb73ab2da1b59b64ea987eedcec /sources/shiboken2 | |
parent | 53a794cb20d00f1b84e601440c32d22e4397d27b (diff) |
shiboken: Add option to skip deprecated functions
Pass the "deprecated" annotation from Clang into the meta language and
reject functions based on it when the the command line option
--skip-deprecated is set.
By default, have Python output a deprecation warning when a deprecated
function is called (which is visible when passing -Wd).
Task-number: PYSIDE-487
Change-Id: Ic28d82963fde11f258b2559b562d3f24017fe98d
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'sources/shiboken2')
9 files changed, 56 insertions, 2 deletions
diff --git a/sources/shiboken2/ApiExtractor/abstractmetabuilder.cpp b/sources/shiboken2/ApiExtractor/abstractmetabuilder.cpp index 7498d3a18..14f6806fe 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetabuilder.cpp +++ b/sources/shiboken2/ApiExtractor/abstractmetabuilder.cpp @@ -112,7 +112,8 @@ static QStringList parseTemplateType(const QString& name) { } AbstractMetaBuilderPrivate::AbstractMetaBuilderPrivate() : m_currentClass(0), - m_logDirectory(QLatin1String(".") + QDir::separator()) + m_logDirectory(QLatin1String(".") + QDir::separator()), + m_skipDeprecated(false) { } @@ -1937,7 +1938,17 @@ AbstractMetaFunction *AbstractMetaBuilderPrivate::traverseFunction(const Functio if (functionItem->isFriend()) return 0; + const bool deprecated = functionItem->isDeprecated(); + if (deprecated && m_skipDeprecated) { + m_rejectedFunctions.insert(originalQualifiedSignatureWithReturn + QLatin1String(" is deprecated."), + AbstractMetaBuilder::GenerationDisabled); + return nullptr; + } + AbstractMetaFunction *metaFunction = new AbstractMetaFunction; + if (deprecated) + *metaFunction += AbstractMetaAttributes::Deprecated; + // Additional check for assignment/move assignment down below metaFunction->setFunctionType(functionTypeFromCodeModel(functionItem->functionType())); metaFunction->setConstant(functionItem->isConstant()); @@ -3075,6 +3086,10 @@ static void writeRejectLogFile(const QString &name, s << "Incompatible API"; break; + case AbstractMetaBuilder::Deprecated: + s << "Deprecated"; + break; + default: s << "unknown reason"; break; @@ -3241,6 +3256,11 @@ void AbstractMetaBuilder::setGlobalHeader(const QString& globalHeader) d->m_globalHeader = QFileInfo(globalHeader); } +void AbstractMetaBuilder::setSkipDeprecated(bool value) +{ + d->m_skipDeprecated = value; +} + void AbstractMetaBuilderPrivate::setInclude(TypeEntry *te, const QString &fileName) const { QFileInfo info(fileName); diff --git a/sources/shiboken2/ApiExtractor/abstractmetabuilder.h b/sources/shiboken2/ApiExtractor/abstractmetabuilder.h index 01806f6b4..7e5c1fc79 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetabuilder.h +++ b/sources/shiboken2/ApiExtractor/abstractmetabuilder.h @@ -53,6 +53,7 @@ public: UnmatchedArgumentType, UnmatchedReturnType, ApiIncompatible, + Deprecated, NoReason }; @@ -87,6 +88,8 @@ public: */ void setGlobalHeader(const QString& globalHeader); + void setSkipDeprecated(bool value); + static AbstractMetaType *translateType(const TypeInfo &_typei, AbstractMetaClass *currentClass = nullptr, bool resolveType = true, diff --git a/sources/shiboken2/ApiExtractor/abstractmetabuilder_p.h b/sources/shiboken2/ApiExtractor/abstractmetabuilder_p.h index 1633bd868..226d6defd 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetabuilder_p.h +++ b/sources/shiboken2/ApiExtractor/abstractmetabuilder_p.h @@ -185,6 +185,7 @@ public: QString m_logDirectory; QFileInfo m_globalHeader; + bool m_skipDeprecated; }; #endif // ABSTRACTMETBUILDER_P_H diff --git a/sources/shiboken2/ApiExtractor/abstractmetalang.h b/sources/shiboken2/ApiExtractor/abstractmetalang.h index bb17ad8d7..1ab3049b2 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetalang.h +++ b/sources/shiboken2/ApiExtractor/abstractmetalang.h @@ -140,7 +140,8 @@ public: OverriddenCppMethod = 0x00800000, FinalCppMethod = 0x01000000, // Add by meta builder (implicit constructors, inherited methods, etc) - AddedMethod = 0x02000000 + AddedMethod = 0x02000000, + Deprecated = 0x04000000 }; Q_DECLARE_FLAGS(Attributes, Attribute) Q_FLAG(Attribute) diff --git a/sources/shiboken2/ApiExtractor/apiextractor.cpp b/sources/shiboken2/ApiExtractor/apiextractor.cpp index 775485c81..7d2ce250e 100644 --- a/sources/shiboken2/ApiExtractor/apiextractor.cpp +++ b/sources/shiboken2/ApiExtractor/apiextractor.cpp @@ -97,6 +97,13 @@ void ApiExtractor::setDebugLevel(ReportHandler::DebugLevel debugLevel) ReportHandler::setDebugLevel(debugLevel); } +void ApiExtractor::setSkipDeprecated(bool value) +{ + m_skipDeprecated = value; + if (m_builder) + m_builder->setSkipDeprecated(m_skipDeprecated); +} + void ApiExtractor::setSuppressWarnings ( bool value ) { TypeDatabase::instance()->setSuppressWarnings(value); @@ -216,6 +223,7 @@ bool ApiExtractor::run() m_builder = new AbstractMetaBuilder; m_builder->setLogDirectory(m_logDirectory); m_builder->setGlobalHeader(m_cppFileName); + m_builder->setSkipDeprecated(m_skipDeprecated); QByteArrayList arguments; arguments.reserve(m_includePaths.size() + 1); for (const HeaderPath &headerPath : qAsConst(m_includePaths)) diff --git a/sources/shiboken2/ApiExtractor/apiextractor.h b/sources/shiboken2/ApiExtractor/apiextractor.h index ab520c9de..3eb90e748 100644 --- a/sources/shiboken2/ApiExtractor/apiextractor.h +++ b/sources/shiboken2/ApiExtractor/apiextractor.h @@ -66,6 +66,7 @@ public: void setCppFileName(const QString& cppFileName); QString cppFileName() const { return m_cppFileName; } void setDebugLevel(ReportHandler::DebugLevel debugLevel); + void setSkipDeprecated(bool value); void setSuppressWarnings(bool value); void setSilent(bool value); void addTypesystemSearchPath(const QString& path); @@ -99,6 +100,7 @@ private: AbstractMetaBuilder* m_builder; QString m_logDirectory; LanguageLevel m_languageLevel = LanguageLevel::Default; + bool m_skipDeprecated = false; // disable copy ApiExtractor(const ApiExtractor&); diff --git a/sources/shiboken2/doc/commandlineoptions.rst b/sources/shiboken2/doc/commandlineoptions.rst index c335fab75..19f614653 100644 --- a/sources/shiboken2/doc/commandlineoptions.rst +++ b/sources/shiboken2/doc/commandlineoptions.rst @@ -69,6 +69,11 @@ Options ``--generation-set`` Generator set to be used (e.g. qtdoc). +.. _skip-deprecated: + +``--skip-deprecated`` + Skip deprecated functions. + .. _diff: ``--diff`` diff --git a/sources/shiboken2/generator/main.cpp b/sources/shiboken2/generator/main.cpp index 362191fd0..c5bcb766d 100644 --- a/sources/shiboken2/generator/main.cpp +++ b/sources/shiboken2/generator/main.cpp @@ -57,6 +57,7 @@ static inline QString typesystemPathOption() { return QStringLiteral("typesystem static inline QString helpOption() { return QStringLiteral("help"); } static inline QString diffOption() { return QStringLiteral("diff"); } static inline QString dryrunOption() { return QStringLiteral("dry-run"); } +static inline QString skipDeprecatedOption() { return QStringLiteral("skip-deprecated"); } static const char helpHint[] = "Note: use --help or -h for more information.\n"; @@ -310,6 +311,8 @@ 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(skipDeprecatedOption(), + QLatin1String("Skip deprecated functions")) << qMakePair(diffOption(), QLatin1String("Print a diff of wrapper files")) << qMakePair(dryrunOption(), @@ -475,6 +478,11 @@ int main(int argc, char *argv[]) // Create and set-up API Extractor ApiExtractor extractor; extractor.setLogDirectory(outputDirectory); + ait = args.find(skipDeprecatedOption()); + if (ait != args.end()) { + extractor.setSkipDeprecated(true); + args.erase(ait); + } ait = args.find(QLatin1String("silent")); if (ait != args.end()) { diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index 9cfc95fd1..a61095e15 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -2656,6 +2656,12 @@ void CppGenerator::writeFunctionCalls(QTextStream &s, const OverloadData &overlo { Indentation indent(INDENT); writeSingleFunctionCall(s, overloadData, func, context); + if (func->attributes().testFlag(AbstractMetaAttributes::Deprecated)) { + s << INDENT << "PyErr_WarnEx(PyExc_DeprecationWarning, \""; + if (auto cls = context.metaClass()) + s << cls->name() << '.'; + s << func->signature() << " is deprecated\", 1);\n"; + } s << INDENT << "break;" << endl; } s << INDENT << '}' << endl; |