From 973aeb422afca93c53b3cc562eca07c4dd479f07 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 10 Sep 2024 07:23:48 -0700 Subject: [PATCH] moc: improve control of Q_UNUSED / (void) in qt_static_metacall() Because I'm pedantic. Change-Id: I3ac643db1a74864f0d69fffdebe7df6c6f1b1c28 Reviewed-by: Christian Ehrlicher (cherry picked from commit e001ae19c700ac694a0ff1fea365e07e6e64f907) Reviewed-by: Qt Cherry-pick Bot --- src/tools/moc/generator.cpp | 42 ++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index 455fcadd29f..fb15a7a5ed6 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -1019,7 +1019,13 @@ void Generator::generateStaticMetacall() fprintf(out, "void %s::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)\n{\n", cdef->qualified.constData()); - bool isUsed_a = false; + enum UsedArgs { + UsedO = 1, + UsedC = 2, + UsedId = 4, + UsedA = 8, + }; + uint usedArgs = 0; const auto generateCtorArguments = [&](int ctorindex) { const FunctionDef &f = cdef->constructorList.at(ctorindex); @@ -1063,7 +1069,7 @@ void Generator::generateStaticMetacall() fprintf(out, " default: break;\n"); fprintf(out, " }\n"); fprintf(out, " }\n"); - isUsed_a = true; + usedArgs |= UsedC | UsedId | UsedA; } QList methodList; @@ -1072,6 +1078,7 @@ void Generator::generateStaticMetacall() methodList += cdef->methodList; if (!methodList.isEmpty()) { + usedArgs |= UsedO | UsedC | UsedId; fprintf(out, " if (_c == QMetaObject::InvokeMetaMethod) {\n"); if (cdef->hasQObject) { #ifndef QT_NO_DEBUG @@ -1097,6 +1104,7 @@ void Generator::generateStaticMetacall() if (f.isRawSlot) { fprintf(out, "QMethodRawArguments{ _a }"); + usedArgs |= UsedA; } else { const auto begin = f.arguments.cbegin(); const auto end = f.arguments.cend(); @@ -1105,7 +1113,7 @@ void Generator::generateStaticMetacall() if (it != begin) fprintf(out, ","); fprintf(out, "(*reinterpret_cast< %s>(_a[%d]))",a.typeNameForCast.constData(), offset++); - isUsed_a = true; + usedArgs |= UsedA; } if (f.isPrivateSignal) { if (!f.arguments.isEmpty()) @@ -1117,7 +1125,7 @@ void Generator::generateStaticMetacall() if (f.normalizedType != "void") { fprintf(out, "\n if (_a[0]) *reinterpret_cast< %s*>(_a[0]) = std::move(_r); } ", noRef(f.normalizedType).constData()); - isUsed_a = true; + usedArgs |= UsedA; } fprintf(out, " break;\n"); } @@ -1151,11 +1159,12 @@ void Generator::generateStaticMetacall() } fprintf(out, " }\n"); fprintf(out, " }\n"); - isUsed_a = true; + usedArgs |= UsedC | UsedId | UsedA; } } if (!cdef->signalList.isEmpty()) { + usedArgs |= UsedC | UsedA; fprintf(out, " if (_c == QMetaObject::IndexOfMethod) {\n"); fprintf(out, " int *result = reinterpret_cast(_a[0]);\n"); bool anythingUsed = false; @@ -1212,7 +1221,7 @@ void Generator::generateStaticMetacall() } fprintf(out, " }\n"); fprintf(out, " }\n"); - isUsed_a = true; + usedArgs |= UsedC | UsedId | UsedA; } if (!cdef->propertyList.empty()) { @@ -1231,6 +1240,10 @@ void Generator::generateStaticMetacall() needReset |= !p.reset.isEmpty(); hasBindableProperties |= !p.bind.isEmpty(); } + if (needGet || needSet || hasBindableProperties || needReset) + usedArgs |= UsedO | UsedC | UsedId; + if (needGet || needSet || hasBindableProperties) + usedArgs |= UsedA; // resetting doesn't need arguments auto setupMemberAccess = [this]() { if (cdef->hasQObject) { @@ -1377,15 +1390,14 @@ void Generator::generateStaticMetacall() } } - if (methodList.isEmpty()) { - fprintf(out, " (void)_o;\n"); - if (cdef->constructorList.isEmpty() && automaticPropertyMetaTypes.isEmpty() && methodsWithAutomaticTypesHelper(methodList).isEmpty()) { - fprintf(out, " (void)_id;\n"); - fprintf(out, " (void)_c;\n"); - } - } - if (!isUsed_a) - fprintf(out, " (void)_a;\n"); + auto printUnused = [&](UsedArgs entry, const char *name) { + if ((usedArgs & entry) == 0) + fprintf(out, " (void)%s;\n", name); + }; + printUnused(UsedO, "_o"); + printUnused(UsedC, "_c"); + printUnused(UsedId, "_id"); + printUnused(UsedA, "_a"); fprintf(out, "}\n"); }