aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/generator
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken2/generator')
-rw-r--r--sources/shiboken2/generator/indentor.h14
-rw-r--r--sources/shiboken2/generator/qtdoc/qtdocgenerator.cpp22
-rw-r--r--sources/shiboken2/generator/shiboken2/cppgenerator.cpp185
-rw-r--r--sources/shiboken2/generator/shiboken2/headergenerator.cpp6
-rw-r--r--sources/shiboken2/generator/shiboken2/shibokengenerator.cpp3
5 files changed, 98 insertions, 132 deletions
diff --git a/sources/shiboken2/generator/indentor.h b/sources/shiboken2/generator/indentor.h
index 111259f12..3dcec85b6 100644
--- a/sources/shiboken2/generator/indentor.h
+++ b/sources/shiboken2/generator/indentor.h
@@ -82,4 +82,18 @@ inline QTextStream &operator <<(QTextStream &s, const IndentorBase<tabWidth> &in
return s;
}
+template <int tabWidth>
+const char *indent(IndentorBase<tabWidth> &indentor)
+{
+ ++indentor.indent;
+ return "";
+}
+
+template <int tabWidth>
+const char *outdent(IndentorBase<tabWidth> &indentor)
+{
+ --indentor.indent;
+ return "";
+}
+
#endif // GENERATOR_H
diff --git a/sources/shiboken2/generator/qtdoc/qtdocgenerator.cpp b/sources/shiboken2/generator/qtdoc/qtdocgenerator.cpp
index 1a7c195b4..282da6eca 100644
--- a/sources/shiboken2/generator/qtdoc/qtdocgenerator.cpp
+++ b/sources/shiboken2/generator/qtdoc/qtdocgenerator.cpp
@@ -1762,8 +1762,7 @@ void QtDocGenerator::writeEnums(QTextStream& s, const AbstractMetaClass* cppClas
{
static const QString section_title = QLatin1String(".. attribute:: ");
- const AbstractMetaEnumList &enums = cppClass->enums();
- for (AbstractMetaEnum *en : enums) {
+ for (AbstractMetaEnum *en : cppClass->enums()) {
s << section_title << cppClass->fullName() << '.' << en->name() << Qt::endl << Qt::endl;
writeFormattedText(s, en->documentation().value(), cppClass);
const auto version = versionOf(en->typeEntry());
@@ -2248,18 +2247,13 @@ void QtDocGenerator::writeModuleDocumentation()
writeFancyToc(s, it.value());
- s << INDENT << ".. container:: hide\n\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << ".. toctree::\n";
- Indentation deeperIndentation(INDENT);
- s << INDENT << ":maxdepth: 1\n\n";
- for (const QString &className : qAsConst(it.value()))
- s << INDENT << className << Qt::endl;
- s << Qt::endl << Qt::endl;
- }
-
- s << "Detailed Description\n--------------------\n\n";
+ s << INDENT << ".. container:: hide\n\n" << indent(INDENT)
+ << INDENT << ".. toctree::\n" << indent(INDENT)
+ << INDENT << ":maxdepth: 1\n\n";
+ for (const QString &className : qAsConst(it.value()))
+ s << INDENT << className << Qt::endl;
+ s << "\n\n" << outdent(INDENT) << outdent(INDENT)
+ << "Detailed Description\n--------------------\n\n";
// module doc is always wrong and C++istic, so go straight to the extra directory!
QFile moduleDoc(m_extraSectionDir + QLatin1Char('/') + moduleName.mid(lastIndex + 1) + QLatin1String(".rst"));
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
index 75633c072..8e9b480cc 100644
--- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
+++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
@@ -1011,11 +1011,8 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s,
s << INDENT << "Shiboken::GilState gil;\n";
// Get out of virtual method call if someone already threw an error.
- s << INDENT << "if (PyErr_Occurred())\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << returnStatement << '\n';
- }
+ s << INDENT << "if (PyErr_Occurred())\n" << indent(INDENT)
+ << INDENT << returnStatement << '\n' << outdent(INDENT);
//PYSIDE-1019: Add info about properties.
int propFlag = 0;
@@ -1035,16 +1032,13 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s,
s << INDENT << "static const char *funcName = \"" << propStr << funcName << "\";\n";
s << INDENT << "Shiboken::AutoDecRef " << PYTHON_OVERRIDE_VAR
<< "(Shiboken::BindingManager::instance().getOverride(this, nameCache, funcName));\n";
- s << INDENT << "if (" << PYTHON_OVERRIDE_VAR << ".isNull()) {\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << "gil.release();\n";
- if (useOverrideCaching(func->ownerClass()))
- s << INDENT << "m_PyMethodCache[" << cacheIndex << "] = true;\n";
- writeVirtualMethodCppCall(s, func, funcName, snips, lastArg, retType,
- returnStatement);
- }
- s << INDENT << "}\n\n"; //WS
+ s << INDENT << "if (" << PYTHON_OVERRIDE_VAR << ".isNull()) {\n"
+ << indent(INDENT) << INDENT << "gil.release();\n";
+ if (useOverrideCaching(func->ownerClass()))
+ s << INDENT << "m_PyMethodCache[" << cacheIndex << "] = true;\n";
+ writeVirtualMethodCppCall(s, func, funcName, snips, lastArg, retType,
+ returnStatement);
+ s << outdent(INDENT) << INDENT << "}\n\n"; //WS
writeConversionRule(s, func, TypeSystem::TargetLangCode);
@@ -1193,15 +1187,14 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s,
}
if (invalidateReturn) {
- s << INDENT << "if (invalidateArg0)\n";
- Indentation indentation(INDENT);
- s << INDENT << "Shiboken::Object::releaseOwnership(" << PYTHON_RETURN_VAR << ".object());\n";
+ s << INDENT << "if (invalidateArg0)\n" << indent(INDENT)
+ << INDENT << "Shiboken::Object::releaseOwnership(" << PYTHON_RETURN_VAR
+ << ".object());\n" << outdent(INDENT);
}
for (int argIndex : qAsConst(invalidateArgs)) {
- s << INDENT << "if (invalidateArg" << argIndex << ")\n";
- Indentation indentation(INDENT);
- s << INDENT << "Shiboken::Object::invalidate(PyTuple_GET_ITEM(" << PYTHON_ARGS << ", ";
- s << (argIndex - 1) << "));\n";
+ s << INDENT << "if (invalidateArg" << argIndex << ")\n" << indent(INDENT)
+ << INDENT << "Shiboken::Object::invalidate(PyTuple_GET_ITEM(" << PYTHON_ARGS
+ << ", " << (argIndex - 1) << "));\n" << outdent(INDENT);
}
@@ -1861,30 +1854,20 @@ void CppGenerator::writeConstructorWrapper(QTextStream &s, const AbstractMetaFun
}
if (metaClass->isAbstract()) {
- s << INDENT << "if (type == myType) {\n";
- {
- Indentation indent(INDENT);
- s << INDENT << "PyErr_SetString(PyExc_NotImplementedError,\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << "\"'" << metaClass->qualifiedCppName();
- }
- s << "' represents a C++ abstract class and cannot be instantiated\");\n";
- s << INDENT << returnStatement(m_currentErrorCode) << Qt::endl;
- }
- s << INDENT<< "}\n\n";
+ s << INDENT << "if (type == myType) {\n" << indent(INDENT)
+ << INDENT << "PyErr_SetString(PyExc_NotImplementedError,\n" << indent(INDENT)
+ << INDENT << "\"'" << metaClass->qualifiedCppName()
+ << "' represents a C++ abstract class and cannot be instantiated\");\n" << outdent(INDENT)
+ << INDENT << returnStatement(m_currentErrorCode) << '\n' << outdent(INDENT)
+ << INDENT<< "}\n\n";
}
if (metaClass->baseClassNames().size() > 1) {
- if (!metaClass->isAbstract()) {
- s << INDENT << "if (type != myType) {\n";
- }
- {
- Indentation indentation(INDENT);
- s << INDENT << "Shiboken::ObjectType::copyMultipleInheritance(type, myType);\n";
- }
if (!metaClass->isAbstract())
- s << INDENT<< "}\n\n";
+ s << INDENT << "if (type != myType)\n" << indent(INDENT);
+ s << INDENT << "Shiboken::ObjectType::copyMultipleInheritance(type, myType);\n";
+ if (!metaClass->isAbstract())
+ s << outdent(INDENT) << '\n';
}
writeMethodWrapperPreamble(s, overloadData, classContext);
@@ -1931,11 +1914,9 @@ void CppGenerator::writeConstructorWrapper(QTextStream &s, const AbstractMetaFun
s << Qt::endl << INDENT << "// QObject setup\n";
s << INDENT << "PySide::Signal::updateSourceObject(self);\n";
s << INDENT << "metaObject = cptr->metaObject(); // <- init python qt properties\n";
- s << INDENT << "if (kwds && !PySide::fillQtProperties(self, metaObject, kwds, argNames, " << argNamesSet.count() << "))\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << returnStatement(m_currentErrorCode) << Qt::endl;
- }
+ s << INDENT << "if (kwds && !PySide::fillQtProperties(self, metaObject, kwds, argNames, "
+ << argNamesSet.count() << "))\n" << indent(INDENT)
+ << INDENT << returnStatement(m_currentErrorCode) << '\n' << outdent(INDENT);
}
// Constructor code injections, position=end
@@ -2203,7 +2184,8 @@ void CppGenerator::writeCppSelfDefinition(QTextStream &s,
Q_ASSERT(!(cppSelfAsReference && hasStaticOverload));
const AbstractMetaClass *metaClass = context.metaClass();
- bool useWrapperClass = avoidProtectedHack() && metaClass->hasProtectedMembers();
+ bool useWrapperClass = avoidProtectedHack() && metaClass->hasProtectedMembers()
+ && !metaClass->attributes().testFlag(AbstractMetaAttributes::FinalCppClass);
Q_ASSERT(!useWrapperClass || context.useWrapper());
QString className;
if (!context.forSmartPointer()) {
@@ -2919,14 +2901,11 @@ void CppGenerator::writeSingleFunctionCall(QTextStream &s,
int numRemovedArgs = OverloadData::numberOfRemovedArguments(func);
- s << INDENT << "if (!PyErr_Occurred()) {\n";
- {
- Indentation indentation(INDENT);
- writeMethodCall(s, func, context, func->arguments().size() - numRemovedArgs);
- if (!func->isConstructor())
- writeNoneReturn(s, func, overloadData.hasNonVoidReturnType());
- }
- s << INDENT << "}\n";
+ s << INDENT << "if (!PyErr_Occurred()) {\n" << indent(INDENT);
+ writeMethodCall(s, func, context, func->arguments().size() - numRemovedArgs);
+ if (!func->isConstructor())
+ writeNoneReturn(s, func, overloadData.hasNonVoidReturnType());
+ s << outdent(INDENT) << INDENT << "}\n";
}
QString CppGenerator::cppToPythonFunctionName(const QString &sourceTypeName, QString targetTypeName)
@@ -3506,14 +3485,16 @@ void CppGenerator::writeMethodCall(QTextStream &s, const AbstractMetaFunction *f
+ QLatin1String(" *>(") + QLatin1String(CPP_SELF_VAR) + QLatin1Char(')');
if (func->isConstant()) {
if (avoidProtectedHack()) {
+ auto ownerClass = func->ownerClass();
mc << "const_cast<const ::";
- if (func->ownerClass()->hasProtectedMembers()) {
+ if (ownerClass->hasProtectedMembers()
+ && !ownerClass->attributes().testFlag(AbstractMetaAttributes::FinalCppClass)) {
// PYSIDE-500: Need a special wrapper cast when inherited
- const QString selfWrapCast = func->ownerClass() == func->implementingClass()
+ const QString selfWrapCast = ownerClass == func->implementingClass()
? QLatin1String(CPP_SELF_VAR)
- : QLatin1String("reinterpret_cast<") + wrapperName(func->ownerClass())
+ : QLatin1String("reinterpret_cast<") + wrapperName(ownerClass)
+ QLatin1String(" *>(") + QLatin1String(CPP_SELF_VAR) + QLatin1Char(')');
- mc << wrapperName(func->ownerClass());
+ mc << wrapperName(ownerClass);
mc << " *>(" << selfWrapCast << ")->";
}
else {
@@ -4555,26 +4536,20 @@ void CppGenerator::writeSetterFunctionPreamble(QTextStream &s, const QString &na
writeCppSelfDefinition(s, context);
- s << INDENT << "if (pyIn == " << NULL_PTR << ") {\n";
- {
- Indentation indent(INDENT);
- s << INDENT << "PyErr_SetString(PyExc_TypeError, \"'";
- s << name << "' may not be deleted\");\n";
- s << INDENT << "return -1;\n";
- }
- s << INDENT << "}\n";
+ s << INDENT << "if (pyIn == " << NULL_PTR << ") {\n" << indent(INDENT)
+ << INDENT << "PyErr_SetString(PyExc_TypeError, \"'"
+ << name << "' may not be deleted\");\n"
+ << INDENT << "return -1;\n"
+ << outdent(INDENT) << INDENT << "}\n";
s << INDENT << "PythonToCppFunc " << PYTHON_TO_CPP_VAR << "{nullptr};\n";
s << INDENT << "if (!";
writeTypeCheck(s, type, QLatin1String("pyIn"), isNumber(type->typeEntry()));
- s << ") {\n";
- {
- Indentation indent(INDENT);
- s << INDENT << "PyErr_SetString(PyExc_TypeError, \"wrong type attributed to '";
- s << name << "', '" << type->name() << "' or convertible type expected\");\n";
- s << INDENT << "return -1;\n";
- }
- s << INDENT<< "}\n\n";
+ s << ") {\n" << indent(INDENT)
+ << INDENT << "PyErr_SetString(PyExc_TypeError, \"wrong type attributed to '"
+ << name << "', '" << type->name() << "' or convertible type expected\");\n"
+ << INDENT << "return -1;\n"
+ << outdent(INDENT) << INDENT<< "}\n\n";
}
void CppGenerator::writeSetterFunction(QTextStream &s,
@@ -5430,8 +5405,7 @@ void CppGenerator::writeInitQtMetaTypeFunctionBody(QTextStream &s, const Generat
}
}
- const AbstractMetaEnumList &enums = metaClass->enums();
- for (AbstractMetaEnum *metaEnum : enums) {
+ for (AbstractMetaEnum *metaEnum : metaClass->enums()) {
if (!metaEnum->isPrivate() && !metaEnum->isAnonymous()) {
for (const QString &name : qAsConst(nameVariants))
s << INDENT << "qRegisterMetaType< ::" << metaEnum->typeEntry()->qualifiedCppName() << " >(\"" << name << "::" << metaEnum->name() << "\");\n";
@@ -5752,7 +5726,7 @@ bool CppGenerator::finishGeneration()
QString signaturesString;
QTextStream signatureStream(&signaturesString);
- Indentation indent(INDENT);
+ Indentation indentation(INDENT);
const auto functionGroups = getGlobalFunctionGroups();
for (auto it = functionGroups.cbegin(), end = functionGroups.cend(); it != end; ++it) {
@@ -5877,19 +5851,13 @@ bool CppGenerator::finishGeneration()
s << "void cleanTypesAttributes(void) {\n";
s << INDENT << "if (PY_VERSION_HEX >= 0x03000000 && PY_VERSION_HEX < 0x03060000)\n";
s << INDENT << " return; // PYSIDE-953: testbinding crashes in Python 3.5 when hasattr touches types!\n";
- s << INDENT << "for (int i = 0, imax = SBK_" << moduleName() << "_IDX_COUNT; i < imax; i++) {\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << "PyObject *pyType = reinterpret_cast<PyObject *>(" << cppApiVariableName() << "[i]);\n";
- s << INDENT << "Shiboken::AutoDecRef attrName(Py_BuildValue(\"s\", \"staticMetaObject\"));\n";
- s << INDENT << "if (pyType && PyObject_HasAttr(pyType, attrName))\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << "PyObject_SetAttr(pyType, attrName, Py_None);\n";
- }
- }
- s << INDENT << "}\n";
- s << "}\n";
+ s << INDENT << "for (int i = 0, imax = SBK_" << moduleName()
+ << "_IDX_COUNT; i < imax; i++) {\n" << indent(INDENT)
+ << INDENT << "PyObject *pyType = reinterpret_cast<PyObject *>(" << cppApiVariableName() << "[i]);\n"
+ << INDENT << "Shiboken::AutoDecRef attrName(Py_BuildValue(\"s\", \"staticMetaObject\"));\n"
+ << INDENT << "if (pyType && PyObject_HasAttr(pyType, attrName))\n" << indent(INDENT)
+ << INDENT << "PyObject_SetAttr(pyType, attrName, Py_None);\n" << outdent(INDENT)
+ << outdent(INDENT) << INDENT << "}\n" << "}\n";
}
s << "// Global functions ";
@@ -6003,19 +5971,15 @@ bool CppGenerator::finishGeneration()
writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning, TypeSystem::TargetLangCode);
for (const QString &requiredModule : requiredModules) {
- s << INDENT << "{\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << "Shiboken::AutoDecRef requiredModule(Shiboken::Module::import(\"" << requiredModule << "\"));\n";
- s << INDENT << "if (requiredModule.isNull())\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << "return SBK_MODULE_INIT_ERROR;\n";
- }
- s << INDENT << cppApiVariableName(requiredModule) << " = Shiboken::Module::getTypes(requiredModule);\n";
- s << INDENT << convertersVariableName(requiredModule) << " = Shiboken::Module::getTypeConverters(requiredModule);\n";
- }
- s << INDENT << "}\n\n";
+ s << INDENT << "{\n" << indent(INDENT)
+ << INDENT << "Shiboken::AutoDecRef requiredModule(Shiboken::Module::import(\"" << requiredModule << "\"));\n"
+ << INDENT << "if (requiredModule.isNull())\n" << indent(INDENT)
+ << INDENT << "return SBK_MODULE_INIT_ERROR;\n" << outdent(INDENT)
+ << INDENT << cppApiVariableName(requiredModule)
+ << " = Shiboken::Module::getTypes(requiredModule);\n"
+ << INDENT << convertersVariableName(requiredModule)
+ << " = Shiboken::Module::getTypeConverters(requiredModule);\n" << outdent(INDENT)
+ << INDENT << "}\n\n";
}
int maxTypeIndex = getMaxTypeIndex() + instantiatedSmartPointers().size();
@@ -6088,13 +6052,10 @@ bool CppGenerator::finishGeneration()
s << INDENT << "Shiboken::Module::registerTypes(module, " << cppApiVariableName() << ");\n";
s << INDENT << "Shiboken::Module::registerTypeConverters(module, " << convertersVariableName() << ");\n";
- s << Qt::endl << INDENT << "if (PyErr_Occurred()) {\n";
- {
- Indentation indentation(INDENT);
- s << INDENT << "PyErr_Print();\n";
- s << INDENT << "Py_FatalError(\"can't initialize module " << moduleName() << "\");\n";
- }
- s << INDENT << "}\n";
+ s << '\n' << INDENT << "if (PyErr_Occurred()) {\n" << indent(INDENT)
+ << INDENT << "PyErr_Print();\n"
+ << INDENT << "Py_FatalError(\"can't initialize module " << moduleName() << "\");\n"
+ << outdent(INDENT) << INDENT << "}\n";
// module inject-code target/end
if (!snips.isEmpty())
diff --git a/sources/shiboken2/generator/shiboken2/headergenerator.cpp b/sources/shiboken2/generator/shiboken2/headergenerator.cpp
index 7826e350a..1fdd89af8 100644
--- a/sources/shiboken2/generator/shiboken2/headergenerator.cpp
+++ b/sources/shiboken2/generator/shiboken2/headergenerator.cpp
@@ -343,8 +343,7 @@ void HeaderGenerator::writeTypeIndexValueLines(QTextStream &s, const AbstractMet
if (!typeEntry->generateCode() || !NamespaceTypeEntry::isVisibleScope(typeEntry))
return;
writeTypeIndexValueLine(s, metaClass->typeEntry());
- const AbstractMetaEnumList &enums = metaClass->enums();
- for (const AbstractMetaEnum *metaEnum : enums) {
+ for (const AbstractMetaEnum *metaEnum : metaClass->enums()) {
if (metaEnum->isPrivate())
continue;
writeTypeIndexValueLine(s, metaEnum->typeEntry());
@@ -503,8 +502,7 @@ bool HeaderGenerator::finishGeneration()
const TypeEntry *classType = metaClass->typeEntry();
includes << classType->include();
- const AbstractMetaEnumList &enums = metaClass->enums();
- for (const AbstractMetaEnum *cppEnum : enums) {
+ for (const AbstractMetaEnum *cppEnum : metaClass->enums()) {
if (cppEnum->isAnonymous() || cppEnum->isPrivate())
continue;
EnumTypeEntry *enumType = cppEnum->typeEntry();
diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp
index 825965b92..1f91609bd 100644
--- a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp
+++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp
@@ -339,8 +339,7 @@ void ShibokenGenerator::lookForEnumsInClassesNotToBeGenerated(AbstractMetaEnumLi
Q_ASSERT(metaClass);
// if a scope is not to be generated, collect its enums into the parent scope
if (!NamespaceTypeEntry::isVisibleScope(metaClass->typeEntry())) {
- const AbstractMetaEnumList &enums = metaClass->enums();
- for (AbstractMetaEnum *metaEnum : enums) {
+ for (AbstractMetaEnum *metaEnum : metaClass->enums()) {
if (!metaEnum->isPrivate() && metaEnum->typeEntry()->generateCode()
&& !enumList.contains(metaEnum)) {
enumList.append(metaEnum);