Skip to content

refactor: replace private access hacks with accessor pattern - #625

Open
zccrs wants to merge 1 commit into
linuxdeepin:masterfrom
zccrs:master
Open

refactor: replace private access hacks with accessor pattern#625
zccrs wants to merge 1 commit into
linuxdeepin:masterfrom
zccrs:master

Conversation

@zccrs

@zccrs zccrs commented May 25, 2026

Copy link
Copy Markdown
Member

Remove all '#define private/protected public' hacks and replace them with a proper C++ template-based private accessor pattern using explicit template instantiation (friend injection trick), mirroring the approach used in linuxdeepin/treeland#875.

Add src/private/dprivateaccessor_p.h with Accessor/AccessorImpl templates and D_DECLARE_PRIVATE_MEMBER, D_DECLARE_PRIVATE_METHOD, D_DECLARE_PRIVATE_CONST_METHOD, D_PRIVATE_MEMBER, D_PRIVATE_CALL macros. All helpers are in global namespace so ADL correctly finds the friend-injected get() function.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @zccrs, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@zccrs
zccrs requested a review from Copilot May 25, 2026 05:13
@deepin-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: zccrs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes preprocessor-based private/protected access hacks and replaces them with a template-based private accessor pattern for reaching Qt private members, while also adding new CI workflows to build the project on Deepin (crimson) and Arch Linux.

Changes:

  • Added a new header implementing an explicit-template-instantiation-based private member accessor and convenience macros.
  • Refactored DQMLGlobalObject to access QSGNode private fields via the new accessor macros instead of #define private public.
  • Updated scenegraph rendering logic in dbackdropnode.cpp and introduced new GitHub Actions build workflows for Deepin and Arch Linux.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/util/dprivateaccessor_p.h Adds the template-based private accessor pattern + macros.
src/private/dqmlglobalobject.cpp Replaces #define private public usage with accessor tags/macros for QSGNode internals.
src/private/dbackdropnode.cpp Removes access-hack macros and updates renderer invocation flow.
.github/workflows/dtkdeclarative-deepin-build.yml Adds Deepin (crimson) container build workflow that builds dependencies from source and produces .deb artifacts.
.github/workflows/dtkdeclarative-archlinux-build.yml Adds Arch Linux container build workflow producing install artifacts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/util/dprivateaccessor_p.h
Comment thread src/util/dprivateaccessor_p.h
Comment thread src/util/dprivateaccessor_p.h
Comment thread .github/workflows/dtkdeclarative-deepin-build.yml
Comment thread .github/workflows/dtkdeclarative-deepin-build.yml
Comment thread .github/workflows/dtkdeclarative-archlinux-build.yml
@zccrs
zccrs force-pushed the master branch 7 times, most recently from 5579d87 to a262b4e Compare May 26, 2026 10:37

#include <algorithm>

#ifndef QT_NO_OPENGL

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个宏判断不需要

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已处理,这个条件编译已经去掉,当前直接保留 static_assert(sizeof(QSGRenderer) == 432),并已随最新 force-push 一起更新。

@zccrs

zccrs commented Jun 1, 2026

Copy link
Copy Markdown
Member Author

已更新这个 PR:\n\n1. 代码已经 rebase 到 upstream 最新,并去掉了混入的 fork master 提交;\n2. dbackdropnode.cpp 已按 Treeland 674e6cfde9925fa2f6b4cb0e49625bee05b8d941 的等价做法改掉;\n3. 刚刚已 force-push 更新分支。

@zccrs

zccrs commented Jun 1, 2026

Copy link
Copy Markdown
Member Author

补充说明一下刚才的更新:\n\n1. 这个 PR 的 head 用的是 zccrs:master,所以我在清理 PR 提交链并 force-push 后,原先混在 master 里的 GitHub Action 提交也一起从该分支历史里拿掉了;\n2. 我已经把原来那条历史备份到了 backup/master-before-pr-cleanup 分支,原有 GitHub Action 改动还在;\n3. 按 Treeland 的做法补了 QSGRenderer bitfield accessor 单测,并新增独立目标 test_qsgrenderer_accessor,本地已通过。

@zccrs

zccrs commented Jun 1, 2026

Copy link
Copy Markdown
Member Author

补充一下 GitHub Action 那条提交的处理:当前 PR 仍保持只包含本次修复,不再把 workflow 提交带回 master;为了保留并方便单独处理,我已经把 workflow 提交单独推到了 ci-workflows-only 分支(另外旧链路也还在 backup/master-before-pr-cleanup)。

@deepin-bot

deepin-bot Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.7.43
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #635

@deepin-bot

deepin-bot Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.7.44
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #641

@deepin-bot

deepin-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.7.45
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #644

@deepin-bot

deepin-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.7.46
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #650

Remove all '#define private/protected public' hacks and replace them with a
proper C++ template-based private accessor pattern using explicit template
instantiation (friend injection trick), mirroring the approach used in
linuxdeepin/treeland#875.

Adds src/util/dprivateaccessor_p.h with Accessor/AccessorImpl templates and
D_DECLARE_PRIVATE_MEMBER, D_PRIVATE_MEMBER macros. All helpers live at global
scope so ADL correctly resolves the friend-injected get() function.
@deepin-ci-robot

Copy link
Copy Markdown
Contributor

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码通过模板显式实例化技术安全访问Qt私有成员,消除了原有的未定义行为风险
逻辑正确且消除了UB,但宏实现较为复杂且存在编译器特性依赖

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

dprivateaccessor_p.h 中的模板和宏定义符合C++标准,利用 [temp.explicit]/12 绕过访问控制是合法的。dbackdropnode.cppdqmlglobalobject.cpp 中的宏调用替换正确。ut_qsgrenderer_accessor.cpp 中使用未构造的内存块进行测试,由于仅进行位域的读写操作,未触发虚函数调用,逻辑上成立。
潜在问题:测试代码 ut_qsgrenderer_accessor.cppreinterpret_cast<QSGRenderer *>(buffer) 未调用构造函数,若未来测试扩展调用了 QSGRenderer 的方法可能引发崩溃;__builtin_offsetof 为 GCC/Clang 特有扩展,若需支持 MSVC 等编译器将导致编译失败。
建议:在测试代码中增加注释说明仅限内存布局测试;考虑提供跨平台的 offsetof 替代方案或限定支持的平台。

  • 2.代码质量(良好)✓

移除了危险的 #define private public,显著提升了代码的健壮性和可维护性。新增的 dprivateaccessor_p.h 头文件注释详尽,解释了技术原理和注意事项。宏命名规范清晰。static_assert(sizeof(QSGRenderer) == 432) 是极佳的防御性编程措施。
潜在问题:宏 D_DECLARE_PRIVATE_BITFIELD 展开后代码较长,可读性一般。
建议:保持现状即可,复杂的宏展开已通过注释进行了充分说明。

  • 3.代码性能(高效)✓

模板显式实例化和友元函数在编译期完成解析,运行时无额外函数调用开销。位域操作通过指针偏移和位运算直接进行内存读写,性能极高。
建议:无需优化。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次代码修改未引入任何外部输入处理,不涉及注入、溢出等安全漏洞。reinterpret_cast 的使用受 static_assert 严格保护,且仅用于内部私有成员访问,不存在安全风险。
建议:无需修复。

■ 【改进建议代码示例】

// tests/ut_qsgrenderer_accessor.cpp
// 增加注释说明未构造对象的安全性
    void init()
    {
        std::memset(buffer, 0, sizeof(buffer));
        // 注意:此处未调用 QSGRenderer 构造函数。
        // 仅用于测试位域内存布局的读写,不可调用涉及虚函数或复杂状态的成员方法。
        renderer = reinterpret_cast<QSGRenderer *>(buffer);
    }

@deepin-bot

deepin-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.7.47
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #654

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants