diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-11 20:20:28 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-15 12:49:00 +0200 |
commit | c16caeb5cb0fbce4a552080d75166caf15374767 (patch) | |
tree | db10eba35f51d34435f32f07b8d8272214a2dfaf | |
parent | cce4684bfd1372822e3fdcb78dda023033c2dad3 (diff) |
shiboken: Refactor ShibokenGenerator::wrapperName()
Assert that it is only used for wrapped classes and remove
fallback path returning the class name, which obfuscates
the code.
Change-Id: I9af1a6a9edc5e566296ec99a50a9f8cfbe055cd0
Reviewed-by: Christian Tismer <tismer@stackless.com>
3 files changed, 26 insertions, 18 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index 34e9cc5eb..c74f4b0a7 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -1364,10 +1364,12 @@ void CppGenerator::writeConverterFunctions(QTextStream &s, const AbstractMetaCla code.clear(); QString computedWrapperName; - if (!classContext.forSmartPointer()) - computedWrapperName = wrapperName(metaClass); - else + if (!classContext.forSmartPointer()) { + computedWrapperName = shouldGenerateCppWrapper(metaClass) + ? wrapperName(metaClass) : metaClass->qualifiedCppName(); + } else { computedWrapperName = wrapperName(classContext.preciseType()); + } c << INDENT << "return Shiboken::Object::newObject(" << cpythonType << ", new ::" << computedWrapperName << "(*reinterpret_cast<const " @@ -3320,13 +3322,15 @@ void CppGenerator::writeMethodCall(QTextStream &s, const AbstractMetaFunction *f } else if (!injectedCodeCallsCppFunction(func)) { if (func->isConstructor()) { isCtor = true; - QString className = wrapperName(func->ownerClass()); + const auto owner = func->ownerClass(); + QString className = shouldGenerateCppWrapper(owner) + ? wrapperName(owner) : owner->qualifiedCppName(); if (func->functionType() == AbstractMetaFunction::CopyConstructorFunction && maxArgs == 1) { mc << "new ::" << className << "(*" << CPP_ARG0 << ')'; } else { QString ctorCall = className + QLatin1Char('(') + userArgs.join(QLatin1String(", ")) + QLatin1Char(')'); - if (usePySideExtensions() && func->ownerClass()->isQObject()) { + if (usePySideExtensions() && owner->isQObject()) { s << INDENT << "void *addr = PySide::nextQObjectMemoryAddr();\n"; uva << "if (addr) {\n"; { @@ -5040,8 +5044,10 @@ void CppGenerator::writeClassRegister(QTextStream &s, s << INDENT; if (!metaClass->isNamespace() && !metaClass->hasPrivateDestructor()) { QString dtorClassName = metaClass->qualifiedCppName(); - if ((avoidProtectedHack() && metaClass->hasProtectedDestructor()) || classTypeEntry->isValue()) + if (((avoidProtectedHack() && metaClass->hasProtectedDestructor()) || classTypeEntry->isValue()) + && shouldGenerateCppWrapper(metaClass)) { dtorClassName = wrapperName(metaClass); + } if (classContext.forSmartPointer()) dtorClassName = wrapperName(classContext.preciseType()); diff --git a/sources/shiboken2/generator/shiboken2/headergenerator.cpp b/sources/shiboken2/generator/shiboken2/headergenerator.cpp index a6f03bb4c..70a24bd3c 100644 --- a/sources/shiboken2/generator/shiboken2/headergenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/headergenerator.cpp @@ -102,7 +102,8 @@ void HeaderGenerator::generateClass(QTextStream &s, GeneratorContext &classConte QString wrapperName; if (!classContext.forSmartPointer()) { - wrapperName = HeaderGenerator::wrapperName(metaClass); + wrapperName = shouldGenerateCppWrapper(metaClass) + ? HeaderGenerator::wrapperName(metaClass) : metaClass->qualifiedCppName(); } else { wrapperName = HeaderGenerator::wrapperName(classContext.preciseType()); } diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp index 47cca8173..543e1c02c 100644 --- a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp @@ -349,15 +349,11 @@ void ShibokenGenerator::lookForEnumsInClassesNotToBeGenerated(AbstractMetaEnumLi QString ShibokenGenerator::wrapperName(const AbstractMetaClass *metaClass) const { - if (shouldGenerateCppWrapper(metaClass)) { - QString result = metaClass->name(); - if (metaClass->enclosingClass()) // is a inner class - result.replace(QLatin1String("::"), QLatin1String("_")); - - result += QLatin1String("Wrapper"); - return result; - } - return metaClass->qualifiedCppName(); + Q_ASSERT(shouldGenerateCppWrapper(metaClass)); + QString result = metaClass->name(); + if (metaClass->enclosingClass()) // is a inner class + result.replace(QLatin1String("::"), QLatin1String("_")); + return result + QLatin1String("Wrapper"); } QString ShibokenGenerator::wrapperName(const AbstractMetaType *metaType) const @@ -1674,7 +1670,9 @@ void ShibokenGenerator::processCodeSnip(QString &code, const AbstractMetaClass * // for the class context in which the variable is used. code.replace(QLatin1String("%PYTHONTYPEOBJECT"), cpythonTypeName(context) + QLatin1String("->type")); - code.replace(QLatin1String("%TYPE"), wrapperName(context)); + const QString className = shouldGenerateCppWrapper(context) + ? wrapperName(context) : context->qualifiedCppName(); + code.replace(QLatin1String("%TYPE"), className); code.replace(QLatin1String("%CPPTYPE"), context->name()); } @@ -2147,7 +2145,10 @@ bool ShibokenGenerator::injectedCodeCallsCppFunction(const AbstractMetaFunction QString wrappedCtorCall; if (func->isConstructor()) { funcCall.prepend(QLatin1String("new ")); - wrappedCtorCall = QStringLiteral("new %1(").arg(wrapperName(func->ownerClass())); + const auto owner = func->ownerClass(); + const QString className = shouldGenerateCppWrapper(owner) + ? wrapperName(owner) : owner->qualifiedCppName(); + wrappedCtorCall = QLatin1String("new ") + className + QLatin1Char('('); } CodeSnipList snips = func->injectedCodeSnips(TypeSystem::CodeSnipPositionAny, TypeSystem::TargetLangCode); for (const CodeSnip &snip : qAsConst(snips)) { |