diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-12-14 14:12:02 +0100 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-12-15 08:23:11 +0100 |
| commit | cf540671a591c446d1d573fc5812aa64102774c6 (patch) | |
| tree | aad8f2f53cc72fda1196b0a09e7d4bf5fbdf2def | |
| parent | 4315cfb44eaa8abd57c685390c6b9d3aff09ba0f (diff) | |
shiboken6: Move parts of the typenameOf() function into libshiboken
The static function causes conflicts with UNITY_BUILDs.
Task-number: PYSIDE-2151
Task-number: PYSIDE-661
Change-Id: Ib0f1ceeb9d393721eab987a0c0419d0a5d0fec45
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 | 31 | ||||
| -rw-r--r-- | sources/shiboken6/libshiboken/helper.cpp | 29 | ||||
| -rw-r--r-- | sources/shiboken6/libshiboken/helper.h | 4 |
3 files changed, 33 insertions, 31 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 56db60861..9408868e9 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -73,31 +73,6 @@ const char *CppGenerator::PYTHON_TO_CPPCONVERSION_STRUCT = "Shiboken::Conversion static inline QString reprFunction() { return QStringLiteral("__repr__"); } -static const char typeNameFunc[] = R"CPP(template <class T> -static const char *typeNameOf(const T &t) -{ - const char *typeName = typeid(t).name(); - auto size = std::strlen(typeName); -#if defined(Q_CC_MSVC) // MSVC: "class QPaintDevice * __ptr64" - if (auto lastStar = strchr(typeName, '*')) { - // MSVC: "class QPaintDevice * __ptr64" - while (*--lastStar == ' ') { - } - size = lastStar - typeName + 1; - } -#else // g++, Clang: "QPaintDevice *" -> "P12QPaintDevice" - if (size > 2 && typeName[0] == 'P' && std::isdigit(typeName[1])) { - ++typeName; - --size; - } -#endif - char *result = new char[size + 1]; - result[size] = '\0'; - memcpy(result, typeName, size); - return result; -} -)CPP"; - TextStream &operator<<(TextStream &s, CppGenerator::ErrorReturn r) { s << "return"; @@ -683,7 +658,7 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon } } - s << '\n' << typeNameFunc << '\n'; + s << '\n'; // class inject-code native/beginning if (!typeEntry->codeSnips().isEmpty()) { @@ -970,7 +945,7 @@ void CppGenerator::generateSmartPointerClass(TextStream &s, const GeneratorConte includes.append(classContext.pointeeClass()->typeEntry()->include()); generateIncludes(s, classContext, {includes}); - s << '\n' << typeNameFunc << '\n'; + s << '\n'; // Create string literal for smart pointer getter method. QString rawGetter = typeEntry->getter(); @@ -1906,7 +1881,7 @@ const char *typeName = )"; c << nameFunc << "(tCppIn);\n"; c << R"(auto sbkType = Shiboken::ObjectType::typeForTypeName(typeName); if (sbkType && Shiboken::ObjectType::hasSpecialCastFunction(sbkType)) { - typeName = typeNameOf(tCppIn); + typeName = Shiboken::typeNameOf(typeid(*tCppIn).name()); changedTypeName = true; } )" diff --git a/sources/shiboken6/libshiboken/helper.cpp b/sources/shiboken6/libshiboken/helper.cpp index 8f836316e..a9ab9eca2 100644 --- a/sources/shiboken6/libshiboken/helper.cpp +++ b/sources/shiboken6/libshiboken/helper.cpp @@ -6,10 +6,13 @@ #include "sbkstring.h" #include "sbkstaticstrings.h" +#include <algorithm> + #include <iomanip> #include <iostream> - +#include <cstring> #include <cstdarg> +#include <cctype> #ifdef _WIN32 # ifndef NOMINMAX @@ -20,8 +23,6 @@ # include <pthread.h> #endif -#include <algorithm> - static void formatPyTypeObject(const PyTypeObject *obj, std::ostream &str) { if (obj) { @@ -442,4 +443,26 @@ ThreadId mainThreadId() return _mainThreadId; } +const char *typeNameOf(const char *typeIdName) +{ + auto size = std::strlen(typeIdName); +#if defined(Q_CC_MSVC) // MSVC: "class QPaintDevice * __ptr64" + if (auto *lastStar = strchr(typeName, '*')) { + // MSVC: "class QPaintDevice * __ptr64" + while (*--lastStar == ' ') { + } + size = lastStar - typeName + 1; + } +#else // g++, Clang: "QPaintDevice *" -> "P12QPaintDevice" + if (size > 2 && typeIdName[0] == 'P' && std::isdigit(typeIdName[1])) { + ++typeIdName; + --size; + } +#endif + char *result = new char[size + 1]; + result[size] = '\0'; + std::memcpy(result, typeIdName, size); + return result; +} + } // namespace Shiboken diff --git a/sources/shiboken6/libshiboken/helper.h b/sources/shiboken6/libshiboken/helper.h index 265bb6581..5063cedcf 100644 --- a/sources/shiboken6/libshiboken/helper.h +++ b/sources/shiboken6/libshiboken/helper.h @@ -36,6 +36,10 @@ LIBSHIBOKEN_API bool listToArgcArgv(PyObject *argList, int *argc, char ***argv, */ LIBSHIBOKEN_API int *sequenceToIntArray(PyObject *obj, bool zeroTerminated = false); +/// Fix a type name returned by typeid(t).name(), depending on compiler. +/// \returns Fixed name (allocated). +LIBSHIBOKEN_API const char *typeNameOf(const char *typeIdName); + /** * Creates and automatically deallocates C++ arrays. */ |
