diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-06-19 17:00:42 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-06-19 21:39:16 +0200 |
commit | 60db641632aaee28fb704a75476381568f17c863 (patch) | |
tree | 487846c7f341f800c34bb49da7580190f3dd126b /sources | |
parent | 42c4179d3cb451db88bfa5d228e81b6b91e553e8 (diff) |
shiboken2: Remove code indenter
With the previous changes fixing the spaces of the
snippets, the indenter can be removed. It just requires
creating local indentors when writing to separate text
streams to avoid unnecessarily indenting by the global
indenter.
Change-Id: Ic9d9661bb1827d40d6f38dc2d47b5fd84729887c
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources')
-rw-r--r-- | sources/shiboken2/generator/generator.cpp | 39 | ||||
-rw-r--r-- | sources/shiboken2/generator/shiboken2/cppgenerator.cpp | 78 |
2 files changed, 47 insertions, 70 deletions
diff --git a/sources/shiboken2/generator/generator.cpp b/sources/shiboken2/generator/generator.cpp index 4eabb3d2c..04658eff3 100644 --- a/sources/shiboken2/generator/generator.cpp +++ b/sources/shiboken2/generator/generator.cpp @@ -540,39 +540,12 @@ void Generator::replaceTemplateVariables(QString &code, const AbstractMetaFuncti QTextStream &formatCode(QTextStream &s, const QString &code, Indentor &indentor) { - // detect number of spaces before the first character - const QStringList lst(code.split(QLatin1Char('\n'))); - static const QRegularExpression nonSpaceRegex(QStringLiteral("[^\\s]")); - Q_ASSERT(nonSpaceRegex.isValid()); - int spacesToRemove = 0; - for (const QString &line : lst) { - if (!line.trimmed().isEmpty()) { - spacesToRemove = line.indexOf(nonSpaceRegex); - if (spacesToRemove == -1) - spacesToRemove = 0; - break; - } - } - - static const QRegularExpression emptyLine(QStringLiteral("^\\s*[\\r]?[\\n]?\\s*$")); - Q_ASSERT(emptyLine.isValid()); - - for (QString line : lst) { - if (line.startsWith(QLatin1Char('#'))) { - s << line; // Do not indent preprocessor lines - } else if (!line.isEmpty() && !emptyLine.match(line).hasMatch()) { - while (line.constEnd()->isSpace()) - line.chop(1); - int limit = 0; - for(int i = 0; i < spacesToRemove; ++i) { - if (!line[i].isSpace()) - break; - limit++; - } - - s << indentor << line.remove(0, limit); - } - s << Qt::endl; + const auto lines= code.splitRef(QLatin1Char('\n')); + for (const auto &line : lines) { + // Do not indent preprocessor lines + if (!line.isEmpty() && !line.startsWith(QLatin1Char('#'))) + s << indentor; + s << line << '\n'; } return s; } diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index e7322be33..0ad4fff3b 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -980,8 +980,9 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, convert = !m_formatUnits.contains(argType->name()); } - Indentation indentation(INDENT); - ac << INDENT; + Indentor nested; + Indentation indentation(nested); + ac << nested; if (!func->conversionRule(TypeSystem::TargetLangCode, arg->argumentIndex() + 1).isEmpty()) { // Has conversion rule. ac << arg->name() + QLatin1String(CONV_RULE_OUT_VAR_SUFFIX); @@ -1221,8 +1222,9 @@ void CppGenerator::writeEnumConverterFunctions(QTextStream &s, const TypeEntry * } QString code; QTextStream c(&code); - c << INDENT << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) =\n" - << INDENT << " "; + Indentor nested; + c << nested << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) =\n" + << nested << " "; if (enumType->isFlags()) c << cppTypeName << "(QFlag(int(PySide::QFlags::getValue(reinterpret_cast<PySideQFlagsObject *>(pyIn)))))"; else @@ -1235,9 +1237,9 @@ void CppGenerator::writeEnumConverterFunctions(QTextStream &s, const TypeEntry * code.clear(); - c << INDENT << "const int castCppIn = int(*reinterpret_cast<const " + c << nested << "const int castCppIn = int(*reinterpret_cast<const " << cppTypeName << " *>(cppIn));\n"; - c << INDENT; + c << nested; c << "return "; if (enumType->isFlags()) { c << "reinterpret_cast<PyObject *>(PySide::QFlags::newObject(castCppIn, " @@ -1262,8 +1264,8 @@ void CppGenerator::writeEnumConverterFunctions(QTextStream &s, const TypeEntry * code.clear(); cppTypeName = getFullTypeName(flags).trimmed(); - c << INDENT << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) =\n" - << INDENT << " " << cppTypeName + c << nested << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) =\n" + << nested << " " << cppTypeName << "(QFlag(int(Shiboken::Enum::getValue(pyIn))));\n"; QString flagsTypeName = fixedCppTypeName(flags); @@ -1271,9 +1273,9 @@ void CppGenerator::writeEnumConverterFunctions(QTextStream &s, const TypeEntry * writeIsPythonConvertibleToCppFunction(s, typeName, flagsTypeName, pyTypeCheck); code.clear(); - c << INDENT << "Shiboken::AutoDecRef pyLong(PyNumber_Long(pyIn));\n"; - c << INDENT << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) =\n" - << INDENT << " " << cppTypeName + c << nested << "Shiboken::AutoDecRef pyLong(PyNumber_Long(pyIn));\n"; + c << nested << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) =\n" + << nested << " " << cppTypeName << "(QFlag(int(PyLong_AsLong(pyLong.object()))));\n"; // PYSIDE-898: Include an additional condition to detect if the type of the // enum corresponds to the object that is being evaluated. @@ -1320,7 +1322,8 @@ void CppGenerator::writeConverterFunctions(QTextStream &s, const AbstractMetaCla QString targetTypeName = metaClass->name() + QLatin1String("_PTR"); QString code; QTextStream c(&code); - c << INDENT << "Shiboken::Conversions::pythonToCppPointer(" << cpythonType << ", pyIn, cppOut);"; + Indentor nested; + c << nested << "Shiboken::Conversions::pythonToCppPointer(" << cpythonType << ", pyIn, cppOut);"; writePythonToCppFunction(s, code, sourceTypeName, targetTypeName); // "Is convertible" function for the Python object to C++ pointer conversion. @@ -1334,30 +1337,30 @@ void CppGenerator::writeConverterFunctions(QTextStream &s, const AbstractMetaCla code.clear(); if (usePySideExtensions() && metaClass->isQObject()) { - c << INDENT << "return PySide::getWrapperForQObject(reinterpret_cast<" + c << nested << "return PySide::getWrapperForQObject(reinterpret_cast<" << typeName << " *>(const_cast<void *>(cppIn)), " << cpythonType << ");\n"; } else { - c << INDENT << "auto pyOut = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper(cppIn));\n"; - c << INDENT << "if (pyOut) {\n"; + c << nested << "auto pyOut = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper(cppIn));\n"; + c << nested << "if (pyOut) {\n"; { - Indentation indent(INDENT); - c << INDENT << "Py_INCREF(pyOut);\n"; - c << INDENT << "return pyOut;\n"; - } - c << INDENT << "}\n"; - c << INDENT << "bool changedTypeName = false;\n" - << INDENT << "auto tCppIn = reinterpret_cast<const " << typeName << " *>(cppIn);\n" - << INDENT << "const char *typeName = typeid(*tCppIn).name();\n" - << INDENT << "auto sbkType = Shiboken::ObjectType::typeForTypeName(typeName);\n" - << INDENT << "if (sbkType && Shiboken::ObjectType::hasSpecialCastFunction(sbkType)) {\n" - << INDENT << " typeName = typeNameOf(tCppIn);\n" - << INDENT << " changedTypeName = true;\n" - << INDENT << "}\n" - << INDENT << "PyObject *result = Shiboken::Object::newObject(" << cpythonType + Indentation indent(nested); + c << nested << "Py_INCREF(pyOut);\n"; + c << nested << "return pyOut;\n"; + } + c << nested << "}\n"; + c << nested << "bool changedTypeName = false;\n" + << nested << "auto tCppIn = reinterpret_cast<const " << typeName << " *>(cppIn);\n" + << nested << "const char *typeName = typeid(*tCppIn).name();\n" + << nested << "auto sbkType = Shiboken::ObjectType::typeForTypeName(typeName);\n" + << nested << "if (sbkType && Shiboken::ObjectType::hasSpecialCastFunction(sbkType)) {\n" + << nested << " typeName = typeNameOf(tCppIn);\n" + << nested << " changedTypeName = true;\n" + << nested << "}\n" + << nested << "PyObject *result = Shiboken::Object::newObject(" << cpythonType << ", const_cast<void *>(cppIn), false, /* exactType */ changedTypeName, typeName);\n" - << INDENT << "if (changedTypeName)\n" - << INDENT << " delete [] typeName;\n" - << INDENT << "return result;"; + << nested << "if (changedTypeName)\n" + << nested << " delete [] typeName;\n" + << nested << "return result;"; } std::swap(targetTypeName, sourceTypeName); writeCppToPythonFunction(s, code, sourceTypeName, targetTypeName); @@ -1387,7 +1390,7 @@ void CppGenerator::writeConverterFunctions(QTextStream &s, const AbstractMetaCla computedWrapperName = classContext.smartPointerWrapperName(); } - c << INDENT << "return Shiboken::Object::newObject(" << cpythonType + c << nested << "return Shiboken::Object::newObject(" << cpythonType << ", new ::" << computedWrapperName << "(*reinterpret_cast<const " << typeName << " *>(cppIn)), true, true);"; writeCppToPythonFunction(s, code, sourceTypeName, targetTypeName); @@ -1410,7 +1413,7 @@ void CppGenerator::writeConverterFunctions(QTextStream &s, const AbstractMetaCla else wrappedCPtrExpression = cpythonWrapperCPtr(classContext.preciseType(), pyInVariable); - c << INDENT << "*reinterpret_cast<" << typeName << " *>(cppOut) = *" + c << nested << "*reinterpret_cast<" << typeName << " *>(cppOut) = *" << wrappedCPtrExpression << ';'; writePythonToCppFunction(s, code, sourceTypeName, targetTypeName); @@ -1479,7 +1482,7 @@ void CppGenerator::writeConverterFunctions(QTextStream &s, const AbstractMetaCla || sourceType->typeEntry()->isEnum() || sourceType->typeEntry()->isFlags()) { QTextStream pc(&toCppPreConv); - pc << INDENT << getFullTypeNameWithoutModifiers(sourceType) << " cppIn"; + pc << nested << getFullTypeNameWithoutModifiers(sourceType) << " cppIn"; writeMinimalConstructorExpression(pc, sourceType); pc << ";\n"; writeToCppConversion(pc, sourceType, nullptr, QLatin1String("pyIn"), QLatin1String("cppIn")); @@ -2952,12 +2955,13 @@ void CppGenerator::writePythonToCppConversionFunctions(QTextStream &s, // Python to C++ conversion function. QString code; QTextStream c(&code); + Indentor nested; if (conversion.isEmpty()) conversion = QLatin1Char('*') + cpythonWrapperCPtr(sourceType->typeEntry(), QLatin1String("pyIn")); if (!preConversion.isEmpty()) - c << INDENT << preConversion << Qt::endl; + c << nested << preConversion << Qt::endl; const QString fullTypeName = getFullTypeName(targetType->typeEntry()); - c << INDENT << "*reinterpret_cast<" << fullTypeName << " *>(cppOut) = " + c << nested << "*reinterpret_cast<" << fullTypeName << " *>(cppOut) = " << fullTypeName << '(' << conversion << ");"; QString sourceTypeName = fixedCppTypeName(sourceType); QString targetTypeName = fixedCppTypeName(targetType); |