From 7a1cae5617ec7d2a9b632554e08c9d30651ea6a6 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sun, 19 Jun 2022 10:18:16 -0700 Subject: [PATCH] moc: properly indent the output for the metatype array Commit 7ff7d73f6ac68227500fa95731ce0f04626e600b added the indentation, but there were a few mistakes and it was off by 4 spaces anyway. So re-do it and take the opportunity to add some comments to explain what we're seeing. Before: qt_incomplete_metaTypeArray, QtPrivate::TypeAndForceComplete, QtPrivate::TypeAndForceComplete, QtPrivate::TypeAndForceComplete, QtPrivate::TypeAndForceComplete, QtPrivate::TypeAndForceComplete, QtPrivate::TypeAndForceComplete , QtPrivate::TypeAndForceComplete, QtPrivate::TypeAndForceComplete, QtPrivate::TypeAndForceComplete, QtPrivate::TypeAndForceComplete >, After: qt_incomplete_metaTypeArray, // property 'interval' QtPrivate::TypeAndForceComplete, // property 'remainingTime' QtPrivate::TypeAndForceComplete, // property 'timerType' QtPrivate::TypeAndForceComplete, // property 'active' QtPrivate::TypeAndForceComplete, // Q_OBJECT / Q_GADGET QtPrivate::TypeAndForceComplete, // method 'timeout' QtPrivate::TypeAndForceComplete, // method 'start' QtPrivate::TypeAndForceComplete, QtPrivate::TypeAndForceComplete, // method 'start' QtPrivate::TypeAndForceComplete, // method 'stop' QtPrivate::TypeAndForceComplete >, Change-Id: Id0fb9ab0089845ee8843fffd16fa152d040ef922 Reviewed-by: Fabian Kosmale Reviewed-by: Friedemann Kleint (cherry picked from commit 3816b14ee8bc6f9722f3b76a32f968e334d0ed27) Reviewed-by: Thiago Macieira --- src/tools/moc/generator.cpp | 67 ++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index bfd22930c59..08146a43476 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -519,63 +519,62 @@ void Generator::generateCode() else fprintf(out, " qt_meta_extradata_%s,\n", qualifiedClassNameIdentifier.constData()); - bool needsComma = false; + const char *comma = ""; const bool requireCompleteness = requireCompleteTypes || cdef->requireCompleteMethodTypes; + auto stringForType = [requireCompleteness](const QByteArray &type, bool forceComplete) -> QByteArray { + const char *forceCompleteType = forceComplete ? ", std::true_type>" : ", std::false_type>"; + if (requireCompleteness) + return type; + return "QtPrivate::TypeAndForceComplete<" % type % forceCompleteType; + }; if (!requireCompleteness) { - fprintf(out, "qt_incomplete_metaTypeArraypropertyList.count(); ++i) { const PropertyDef &p = cdef->propertyList.at(i); - if (needsComma) - fputs(",\n ", out); - if (requireCompleteness) - fputs(p.type.data(), out); - else - fprintf(out, "QtPrivate::TypeAndForceComplete<%s, std::true_type>", p.type.data()); - needsComma = true; + fprintf(out, "%s\n // property '%s'\n %s", + comma, p.name.constData(), stringForType(p.type, true).constData()); + comma = ","; } + // type name for the Q_OJBECT/GADGET itself, void for namespaces auto ownType = !cdef->hasQNamespace ? cdef->classname.data() : "void"; - if (needsComma) - fputs(",\n ", out); - if (requireCompleteness) - fputs(ownType, out); - else - fprintf(out, "QtPrivate::TypeAndForceComplete<%s, std::true_type>", ownType); + fprintf(out, "%s\n // Q_OBJECT / Q_GADGET\n %s", + comma, stringForType(ownType, true).constData()); + comma = ","; // metatypes for all exposed methods - // no need to check for needsComma any longer, as we always need one due to the classname being present + // because we definitely printed something above, this section doesn't need comma control for (const QList &methodContainer : { cdef->signalList, cdef->slotList, cdef->methodList }) { for (int i = 0; i< methodContainer.count(); ++i) { const FunctionDef& fdef = methodContainer.at(i); - if (requireCompleteness) - fprintf(out, ",\n %s", fdef.type.name.data()); - else - fprintf(out, ",\n QtPrivate::TypeAndForceComplete<%s, std::false_type>", fdef.type.name.data()); - for (const auto &argument: fdef.arguments) { - if (requireCompleteness) - fprintf(out, ", %s", argument.type.name.data()); - else - fprintf(out, ",\n QtPrivate::TypeAndForceComplete<%s, std::false_type>", argument.type.name.data()); - } + fprintf(out, ",\n // method '%s'\n %s", + fdef.name.constData(), stringForType(fdef.type.name, false).constData()); + for (const auto &argument: fdef.arguments) + fprintf(out, ",\n %s", stringForType(argument.type.name, false).constData()); } - fprintf(out, "\n"); } + + // but constructors have no return types, so this needs comma control again for (int i = 0; i< cdef->constructorList.count(); ++i) { const FunctionDef& fdef = cdef->constructorList.at(i); + if (fdef.arguments.isEmpty()) + continue; + + fprintf(out, "%s\n // constructor '%s'", comma, fdef.name.constData()); + comma = ""; for (const auto &argument: fdef.arguments) { - if (requireCompleteness) - fprintf(out, ",\n %s", argument.type.name.data()); - else - fprintf(out, ",\n QtPrivate::TypeAndForceComplete<%s, std::false_type>", argument.type.name.data()); + fprintf(out, "%s\n %s", comma, + stringForType(argument.type.name, false).constData()); + comma = ","; } } - fprintf(out, ">,\n"); + fprintf(out, "\n >,\n"); fprintf(out, " nullptr\n} };\n\n");