diff options
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.cpp | 9 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/headergenerator.cpp | 9 | ||||
-rw-r--r-- | sources/shiboken6/generator/shiboken/headergenerator.h | 2 |
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; |