diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2024-01-31 13:53:24 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2024-01-31 16:13:11 +0100 |
commit | 866f6620aaa852c6fafb4b96625017ad44ce0dcb (patch) | |
tree | 6818c3e9796752dc39829903c43b265c178ab0dc /sources/shiboken6/generator/shiboken/cppgenerator.cpp | |
parent | f9e885010a55883a066c01b3ee48266b0a919df3 (diff) |
shiboken6: Improve code snippet error handling
Type conversion errors in code snippets can be hard to diagnose,
particularly when they occur in an invalid context due to misspelt
snippet markers.
Add some functions wrapping the in try/catch, adding some context
information to the error messages.
Pick-to: 6.6
Change-Id: I7e35f298497b7fd0b582f43d6941a683e18377b3
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/shiboken6/generator/shiboken/cppgenerator.cpp')
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.cpp | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index a2cb6399e..efb7cfba9 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -473,6 +473,19 @@ void CppGenerator::writePyMethodDefs(TextStream &s, const QString &className, << methodsDefinitions << METHOD_DEF_SENTINEL << outdent << "};\n\n"; } +void CppGenerator::writeModuleCodeSnips(TextStream &s, const CodeSnipList &codeSnips, + TypeSystem::CodeSnipPosition position, + TypeSystem::Language language) const +{ + if (!codeSnips.isEmpty()) { + try { + writeCodeSnips(s, codeSnips, position, language); + } catch (const std::exception &e) { + throw Exception(msgSnippetError("module source of "_L1 + moduleName(), e.what())); + } + } +} + bool CppGenerator::hasHashFunction(const AbstractMetaClassCPtr &c) { return !c->typeEntry()->hashFunction().isEmpty() @@ -3112,9 +3125,10 @@ void CppGenerator::writeCppToPythonFunction(TextStream &s, const QString &code, { QString prettyCode = code; - processCodeSnip(prettyCode); + const QString funcName = cppToPythonFunctionName(sourceTypeName, targetTypeName); + processCodeSnip(prettyCode, funcName); - s << "static PyObject *" << cppToPythonFunctionName(sourceTypeName, targetTypeName) + s << "static PyObject *" << funcName << "(const void *cppIn)\n{\n" << indent << prettyCode << ensureEndl << outdent << "}\n"; } @@ -3185,7 +3199,7 @@ void CppGenerator::writeCppToPythonFunction(TextStream &s, const AbstractMetaTyp code.replace(u"%INTYPE_"_s + QString::number(i), typeName); } replaceCppToPythonVariables(code, getFullTypeNameWithoutModifiers(containerType), true); - processCodeSnip(code); + processCodeSnip(code, containerType.typeEntry()->qualifiedCppName()); writeCppToPythonFunction(s, code, fixedCppTypeName(containerType), containerNativeToTargetTypeName(cte)); } @@ -3194,8 +3208,9 @@ void CppGenerator::writePythonToCppFunction(TextStream &s, const QString &code, const QString &targetTypeName) const { QString prettyCode = code; - processCodeSnip(prettyCode); - s << "static void " << pythonToCppFunctionName(sourceTypeName, targetTypeName) + const QString funcName = pythonToCppFunctionName(sourceTypeName, targetTypeName); + processCodeSnip(prettyCode, funcName); + s << "static void " << funcName << "(PyObject *pyIn, void *cppOut)\n{\n" << indent << prettyCode << ensureEndl << outdent << "}\n"; } @@ -3298,7 +3313,7 @@ void CppGenerator::writePythonToCppConversionFunctions(TextStream &s, + cpythonTypeNameExt(toNative.sourceType()) + u')'; } typeCheck.replace(u"%in"_s, u"pyIn"_s); - processCodeSnip(typeCheck); + processCodeSnip(typeCheck, targetType->qualifiedCppName()); writeIsPythonConvertibleToCppFunction(s, sourceTypeName, targetTypeName, typeCheck); } @@ -6041,8 +6056,7 @@ bool CppGenerator::finishGeneration() const CodeSnipList snips = moduleEntry->codeSnips(); // module inject-code native/beginning - if (!snips.isEmpty()) - writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning, TypeSystem::NativeCode); + writeModuleCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning, TypeSystem::NativeCode); // cleanup staticMetaObject attribute if (usePySideExtensions()) { @@ -6172,8 +6186,8 @@ bool CppGenerator::finishGeneration() << indent << "return " << globalModuleVar << ";\n" << outdent; // module inject-code target/beginning - if (!snips.isEmpty()) - writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning, TypeSystem::TargetLangCode); + writeModuleCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning, + TypeSystem::TargetLangCode); for (const QString &requiredModule : requiredModules) { s << "{\n" << indent @@ -6297,12 +6311,10 @@ bool CppGenerator::finishGeneration() << outdent << "}\n"; // module inject-code target/end - if (!snips.isEmpty()) - writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionEnd, TypeSystem::TargetLangCode); + writeModuleCodeSnips(s, snips, TypeSystem::CodeSnipPositionEnd, TypeSystem::TargetLangCode); // module inject-code native/end - if (!snips.isEmpty()) - writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionEnd, TypeSystem::NativeCode); + writeModuleCodeSnips(s, snips, TypeSystem::CodeSnipPositionEnd, TypeSystem::NativeCode); if (usePySideExtensions()) { for (const AbstractMetaEnum &metaEnum : std::as_const(globalEnums)) |