我有以下C++代码:
#include<stdexcept>
#include<string>
#include<cassert>
class my_exception_t : public std::runtime_error {
public:
my_exception_t();
~my_exception_t() noexcept override;
};
class my_other_exception_t : public std::runtime_error {
public:
my_other_exception_t();
~my_other_exception_t() noexcept override;
};
class my_sub_exception_t : public my_exception_t {
public:
my_sub_exception_t();
~my_sub_exception_t() noexcept override;
};
my_exception_t::my_exception_t()
: std::runtime_error("my-exception") {}
my_exception_t::~my_exception_t() noexcept = default;
my_other_exception_t::my_other_exception_t()
: std::runtime_error("my-other-exception") {}
my_other_exception_t::~my_other_exception_t() noexcept = default;
my_sub_exception_t::my_sub_exception_t() : my_exception_t{} {}
my_sub_exception_t::~my_sub_exception_t() noexcept = default;
int main() {
//TEST 1
static_assert(std::is_base_of<std::runtime_error,
my_exception_t>::value);
my_exception_t me;
//TEST 2
static_assert(std::is_base_of<std::runtime_error,
my_other_exception_t>::value);
my_other_exception_t moe;
//TEST 3
static_assert(std::is_base_of<std::runtime_error,
my_sub_exception_t>::value);
static_assert(std::is_base_of<my_exception_t,
my_sub_exception_t>::value);
my_sub_exception_t mse;
}它像预期的那样工作,但我也想检查代码覆盖率!但是当我使用clang++-11和标志-O2编译代码,执行二进制文件并在其上运行govr时,我可以看到包含析构~my_sub_exception()定义的行没有被覆盖:
我正在执行以下调用:
$ clang++-11 -O2 -coverage -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded -Werror -pedantic-errors -Wno-global-constructors -std=c++17 -o ex.cpp.o -c ex.cpp
$ clang++-11 ex.cpp.o -o ex -coverage
$ ./ex
$ gcovr -r . --gcov-executable="llvm-cov-11 gcov"
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Lines Exec Cover Missing
------------------------------------------------------------------------------
ex.cpp 13 12 92% 32
------------------------------------------------------------------------------
TOTAL 13 12 92%
------------------------------------------------------------------------------注意:如果我在-O0中使用clang++-11,我将获得完整的覆盖范围。(如果我将g++-11与-O2和-O0一起使用,我也可以获得完整的覆盖范围)。但为什么不使用带有-O2的clang++-11
就我观察到的问题而言,我只为继承自my_exception_t的异常得到了这行未覆盖的代码。如果异常类是从std::runtime_error继承的,则不会缺少任何行覆盖率。
发布于 2021-07-25 05:55:00
也许是因为所有的my_exception都是全局(静态)变量,因此不需要销毁任何东西(当进程结束时,它们就会消失)。
要真正回答这个问题,你必须雇佣你自己的C++语言律师和clang++专家。
https://stackoverflow.com/questions/68513985
复制相似问题