You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在gcovr中排除特定行的‘未执行分支’报告?

如何排除g++隐式生成代码导致的gcovr分支覆盖误报?

我正在开发一个C++项目,使用g++ --coverage编译,通过单元测试生成覆盖率指标,并借助gcovr转换为HTML报告。但分支覆盖信息存在误导性,大量“未执行分支”结果来自编译器隐式生成的代码,例如:

class Derived : public AbstractBase {
public:
    Derived(SharedObjectPtr sharedObjectPtr, const OtherParameters& otherParameters)
        : AbstractBase(sharedObjectPtr, otherParameters) {}; // <-- Branch 3 not taken
};

以及:

class SpecialList {
private:
    unsigned length;
public:
    SpecialList() : length(0) {};
    ~SpecialList() {}; // <-- Branch 1 not taken
};

为这些隐式分支编写测试代码并不合理,我们希望排除这类“未执行分支”结果,让覆盖率报告准确反映源码的分支覆盖情况。gcovr文档提到了exclude-branches-by-pattern选项,但未给出示例,我在gcovr.cfg文件中添加了:

exclude-branches-by-pattern = ~SpecialList

但问题仍存在。当前使用g++ 13.2.0和gcovr 7.0,后续还尝试在gcovr命令行添加--exclude-throw-branches参数,问题依旧。请问如何排除特定行的“未执行分支”报告?

内容的提问来源于stack exchange,提问作者Lee Jenkins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 06:05:03