QQmlSA::ElementPass Class
class QQmlSA::ElementPass所有元素静态分析传递的基类。更多
头文件: | #include <ElementPass> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS QmlCompiler) target_link_libraries(mytarget PRIVATE Qt6::QmlCompiler) |
继承: | QQmlSA::GenericPass |
状态: | 技术预览 |
公共函数
ElementPass(QQmlSA::PassManager *manager) | |
virtual void | run(const QQmlSA::Element &element) = 0 |
virtual bool | shouldRun(const QQmlSA::Element &element) |
详细说明
ElementPass 是两个分析程序中较为简单的一个。它将考虑文件中的每个元素。shouldRun() 方法可用于过滤掉不相关的元素,而run() 方法正在进行初始工作。
适合 ElementPass 的常见任务有
- 检查元素属性的组合方式是否不合理
- 验证属性值(例如,属性只接受特定的枚举值)
- 检查依赖于元素父元素的行为(例如,当父元素是
Layout
时,不使用Item::width )。
如下图所示,建议在 ElementPass 的构造函数中进行必要的类型解析,并将其缓存在本地成员中,同时通过shouldRun() 实现一些过滤功能,以保持静态分析的性能。
using namespace QQmlSA; class MyElementPass : public ElementPass { Element myType; public: MyElementPass(QQmlSA::PassManager *manager) : myType(resolveType("MyModule", "MyType")) {} bool shouldRun(const Element &element) override { return element.inherits(myType); } void run(const Element &element) override { // actual pass logic } }
ElementPass 对元素属性的使用情况了解有限。如果需要这些信息,请考虑使用PropertyPass 。
注意: ElementPass 只考虑可实例化的类型。因此,它不适合分析附加类型和单子。这些需要通过PropertyPass 来处理。
成员函数文档
ElementPass::ElementPass(QQmlSA::PassManager *manager)
创建 ElementPass 对象,并使用manager 引用通行证管理器。
[pure virtual]
void ElementPass::run(const QQmlSA::Element &element)
如果shouldRun()
返回true
则执行。在element 上执行传递的实际计算。此方法可以重载。无需调用基本方法。
[virtual]
bool ElementPass::shouldRun(const QQmlSA::Element &element)
控制是否在给定的element 上执行run()
函数。子类可以覆盖此方法,通过过滤掉不相关的元素来提高分析性能。
默认实现无条件返回true
。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.