aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2023-06-27 13:05:28 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-06-27 13:48:33 +0000
commit65f1e91dc486a2e5a561805db19122f14a60dbe4 (patch)
treeaf73942870e590393f427d42766d2c5a677595b1
parent3e8b39e1c98263578e1205080382142bdc4b3bc7 (diff)
shiboken6: Generate the protected hack define into all files
The protected hack define is only required for classes for which we want to generate protected API, but it needs to be generated into all files to ensure ODR for Unity builds. Task-number: PYSIDE-2155 Change-Id: I0ee622816886960b8c2c9f319d1a6eb5e8ff4d26 Reviewed-by: Christian Tismer <tismer@stackless.com> (cherry picked from commit 48ec89542f00e041a9ff8da63bcc890f87c0c476) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp9
-rw-r--r--sources/shiboken6/generator/shiboken/headergenerator.cpp9
-rw-r--r--sources/shiboken6/generator/shiboken/headergenerator.h2
3 files changed, 14 insertions, 6 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
index 38e37c272..6b8d76bfc 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
@@ -504,11 +504,10 @@ void CppGenerator::generateIncludes(TextStream &s, const GeneratorContext &class
s << licenseComment() << '\n';
const bool normalClass = !classContext.forSmartPointer();
- if (normalClass && !avoidProtectedHack() && !metaClass->isNamespace()
- && !metaClass->hasPrivateDestructor()) {
- s << "//workaround to access protected functions\n";
- s << "#define protected public\n\n";
- }
+ // Normally only required for classes for which we want to generate protected API,
+ // but it needs to be generated into all files to ensure ODR for Unity builds.
+ if (!avoidProtectedHack())
+ s << HeaderGenerator::protectedHackDefine;
QByteArrayList cppIncludes{"typeinfo", "iterator", // for containers
"cctype", "cstring"};
diff --git a/sources/shiboken6/generator/shiboken/headergenerator.cpp b/sources/shiboken6/generator/shiboken/headergenerator.cpp
index c7039d2b1..4d02bda3d 100644
--- a/sources/shiboken6/generator/shiboken/headergenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/headergenerator.cpp
@@ -63,6 +63,13 @@ static bool alwaysGenerateDestructorDeclaration()
return clang::compiler() == Compiler::Msvc;
}
+const char *HeaderGenerator::protectedHackDefine = R"(// Workaround to access protected functions
+#ifndef protected
+# define protected public
+#endif
+
+)";
+
QString HeaderGenerator::fileNameForContext(const GeneratorContext &context) const
{
return headerFileNameForContext(context);
@@ -99,7 +106,7 @@ void HeaderGenerator::generateClass(TextStream &s, const GeneratorContext &class
s << "#define SBK_" << outerHeaderGuard << "_H\n\n";
if (!avoidProtectedHack())
- s << "#define protected public\n\n";
+ s << protectedHackDefine;
//Includes
s << metaClass->typeEntry()->include() << '\n';
diff --git a/sources/shiboken6/generator/shiboken/headergenerator.h b/sources/shiboken6/generator/shiboken/headergenerator.h
index 9fb0c3d95..6ebcc04ec 100644
--- a/sources/shiboken6/generator/shiboken/headergenerator.h
+++ b/sources/shiboken6/generator/shiboken/headergenerator.h
@@ -22,6 +22,8 @@ public:
const char *name() const override { return "Header generator"; }
+ static const char *protectedHackDefine;
+
protected:
QString fileNameForContext(const GeneratorContext &context) const override;
void generateClass(TextStream &s, const GeneratorContext &classContext) override;