diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-11-28 11:28:04 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-11-30 16:00:15 +0100 |
commit | a997326ff9beef12bb0f99d057cd5a48a7f4549b (patch) | |
tree | d68575e2d081aed5e3f235c189969e138d2b8375 | |
parent | 74b22de599be394f52280e59423f63f78159fa87 (diff) |
shiboken6: Fix warning about nonreachable code in generated rich comparison
Remove the generated goto after the return from rich comparison.
As this introduces a new warning about the then unused error label,
write the error label only when needed.
Amends c7904338f8707a30c70f1ddf62ec740cae255f36.
Spotted in WASM builds which uses -Wunreachable-code.
Task-number: PYSIDE-74
Pick-to: 6.4 6.2 5.15
Change-Id: I293aee5b28631c0127a7de197812d77504a61e24
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.cpp | 25 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.h | 3 |
2 files changed, 19 insertions, 9 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 1b6c46f57..c59cfbc11 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -5275,6 +5275,11 @@ void CppGenerator::writeRichCompareFunctionHeader(TextStream &s, << sbkUnusedVariableCast(PYTHON_TO_CPP_VAR) << '\n'; } +static bool containsGoto(const CodeSnip &s) +{ + return s.code().contains(u"goto"); +} + static const char richCompareComment[] = "// PYSIDE-74: By default, we redirect to object's tp_richcompare (which is `==`, `!=`).\n"; @@ -5288,6 +5293,7 @@ void CppGenerator::writeRichCompareFunction(TextStream &s, s << "switch (op) {\n" << indent; const QList<AbstractMetaFunctionCList> &groupedFuncs = filterGroupedOperatorFunctions(metaClass, OperatorQueryOption::ComparisonOp); + bool needErrorLabel = false; for (const AbstractMetaFunctionCList &overloads : groupedFuncs) { const auto rfunc = overloads[0]; @@ -5333,6 +5339,7 @@ void CppGenerator::writeRichCompareFunction(TextStream &s, TypeSystem::TargetLangCode, func, false /* uses PyArgs */, &func->arguments().constLast()); generateOperatorCode = false; + needErrorLabel |= std::any_of(snips.cbegin(), snips.cend(), containsGoto); } } if (generateOperatorCode) { @@ -5367,6 +5374,7 @@ void CppGenerator::writeRichCompareFunction(TextStream &s, << "Py_INCREF(" << PYTHON_RETURN_VAR << ");\n" << outdent; } else { s << indent << "goto " << baseName << "_RichComparison_TypeError;\n" << outdent; + needErrorLabel = true; } s << "}\n\n"; @@ -5375,19 +5383,20 @@ void CppGenerator::writeRichCompareFunction(TextStream &s, s << "default:\n" << indent << richCompareComment << "return FallbackRichCompare(self, " << PYTHON_ARG << ", op);\n" - << "goto " << baseName << "_RichComparison_TypeError;\n" << outdent - << outdent << "}\n\n"; + << outdent << outdent << "}\n\n"; - writeRichCompareFunctionFooter(s, baseName); + writeRichCompareFunctionFooter(s, baseName, needErrorLabel); } void CppGenerator::writeRichCompareFunctionFooter(TextStream &s, - const QString &baseName) + const QString &baseName, + bool writeErrorLabel) { s << "if (" << PYTHON_RETURN_VAR << " && !PyErr_Occurred())\n" << indent - << "return " << PYTHON_RETURN_VAR << ";\n" << outdent - << baseName << "_RichComparison_TypeError:\n" - << "Shiboken::Errors::setOperatorNotImplemented();\n" + << "return " << PYTHON_RETURN_VAR << ";\n" << outdent; + if (writeErrorLabel) + s << baseName << "_RichComparison_TypeError:\n"; + s << "Shiboken::Errors::setOperatorNotImplemented();\n" << ErrorReturn::Default << '\n' << outdent << "}\n\n"; } @@ -5498,7 +5507,7 @@ void CppGenerator::writeSmartPointerRichCompareFunction(TextStream &s, << "goto " << baseName << "_RichComparison_TypeError;\n" << outdent << "}\n"; - writeRichCompareFunctionFooter(s, baseName); + writeRichCompareFunctionFooter(s, baseName, true); } // Return a flag combination for PyMethodDef diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.h b/sources/shiboken6/generator/shiboken/cppgenerator.h index b2e99b7bb..f16f5cce4 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.h +++ b/sources/shiboken6/generator/shiboken/cppgenerator.h @@ -427,7 +427,8 @@ private: const QString &baseName, const GeneratorContext &context) const; static void writeRichCompareFunctionFooter(TextStream &s, - const QString &baseName); + const QString &baseName, + bool writeLabel); void writeRichCompareFunction(TextStream &s, const GeneratorContext &context) const; void writeSmartPointerRichCompareFunction(TextStream &s, const GeneratorContext &context) const; |