aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp31
-rw-r--r--sources/shiboken6/libshiboken/helper.cpp29
-rw-r--r--sources/shiboken6/libshiboken/helper.h4
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.
*/