首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >clang-11 (优化版本):为什么(用-O2编译的) C++代码的gcovr行覆盖率不是100%

clang-11 (优化版本):为什么(用-O2编译的) C++代码的gcovr行覆盖率不是100%
EN

Stack Overflow用户
提问于 2021-07-25 05:37:19
回答 1查看 75关注 0票数 0

我有以下C++代码:

代码语言:javascript
复制
 #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()定义的行没有被覆盖:

我正在执行以下调用:

代码语言:javascript
复制
$ 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继承的,则不会缺少任何行覆盖率。

EN

回答 1

Stack Overflow用户

发布于 2021-07-25 05:55:00

也许是因为所有的my_exception都是全局(静态)变量,因此不需要销毁任何东西(当进程结束时,它们就会消失)。

要真正回答这个问题,你必须雇佣你自己的C++语言律师和clang++专家。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68513985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档