From 0cf0a2e44cd005121fdd0f32e5bc3280d719a026 Mon Sep 17 00:00:00 2001 From: Oliver Lee Date: Sat, 23 Dec 2023 23:46:32 -0800 Subject: [PATCH] remove exception branches from coverage Disable exceptions to reduce "hidden branches" analyzed by gcov. These branches are not a realistic indicator of coverage as this library does not explicitly allocate memory, throw exceptions, or catch exceptions. Change-Id: I6df51846f4e3da70cc2d53b6e4e6e2b9be36a2f7 --- .bazelrc | 3 --- rules/lcov.bzl | 2 -- test/expect_message_test.cpp | 6 +++--- tools/BUILD.bazel | 28 ++++++++++++++++++++++++---- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/.bazelrc b/.bazelrc index f2c0d40..5f1d4a8 100644 --- a/.bazelrc +++ b/.bazelrc @@ -25,7 +25,4 @@ build:verbose-clang-tidy --@bazel_clang_tidy//:clang_tidy_executable=//tools:ver build:clang-tidy --config=clang-tidy-base build:clang-tidy --@bazel_clang_tidy//:clang_tidy_executable=@llvm_toolchain//:clang-tidy -# Clang will fail for tests that replace malloc and has worse coverage results than GCC -coverage --extra_toolchains=//toolchain:gcc - try-import %workspace%/user.bazelrc diff --git a/rules/lcov.bzl b/rules/lcov.bzl index 2d80b27..a5a5246 100644 --- a/rules/lcov.bzl +++ b/rules/lcov.bzl @@ -13,8 +13,6 @@ def lcov( "--combined_report=lcov", "--experimental_generate_llvm_lcov", "--test_output=errors", - # https://github.com/bazelbuild/bazel/issues/13919 - "--test_env=COVERAGE_GCOV_OPTIONS=-b", ], lcov_tool = "lcov", lcov_opts = []): diff --git a/test/expect_message_test.cpp b/test/expect_message_test.cpp index 33fa405..5b37228 100644 --- a/test/expect_message_test.cpp +++ b/test/expect_message_test.cpp @@ -1,5 +1,7 @@ #include "skytest/skytest.hpp" +#include + auto main() -> int { using namespace ::skytest::literals; @@ -18,8 +20,6 @@ auto main() -> int }; "not invoked on success"_test = [] { - return expect(true, [](auto&) { - throw std::runtime_error{"should not be invoked"}; - }); + return expect(true, [](auto&) { std::terminate(); }); }; } diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel index 061888e..dbc64d7 100644 --- a/tools/BUILD.bazel +++ b/tools/BUILD.bazel @@ -110,27 +110,47 @@ incompatible_with_sanitizers( deps = ["//tools:maybe_incompatible_clang"], ) +lcov_attrs = { + "instrumented_targets": ["//:skytest"], + "test_targets": ["//test/..."], + "coverage_opts": [ + "--combined_report=lcov", + "--experimental_generate_llvm_lcov", + "--test_output=errors", + "--extra_toolchains=//toolchain:gcc", + # https://github.com/bazelbuild/bazel/issues/13919 + # https://github.com/bazelbuild/bazel/blob/master/tools/test/collect_cc_coverage.sh + "--test_env=COVERAGE_GCOV_OPTIONS=-b", + # https://gcovr.com/en/stable/faq.html#why-does-c-code-have-so-many-uncovered-branches + # https://stackoverflow.com/questions/42003783/lcov-gcov-branch-coverage-with-c-producing-branches-all-over-the-place + "--cxxopt=-fno-exceptions", + ], +} + lcov( name = "lcov_list", - instrumented_targets = ["//:skytest"], + coverage_opts = lcov_attrs["coverage_opts"], + instrumented_targets = lcov_attrs["instrumented_targets"], lcov_opts = [ "--rc lcov_branch_coverage=1", "--list", ], - test_targets = ["//test/..."], + test_targets = lcov_attrs["test_targets"], ) lcov( name = "lcov_html", - instrumented_targets = ["//:skytest"], + coverage_opts = lcov_attrs["coverage_opts"], + instrumented_targets = lcov_attrs["instrumented_targets"], lcov_opts = [ "--show-details", "--keep-descriptions", "--branch-coverage", "--highlight", + "--demangle-cpp", "--missed", "--dark-mode", ], lcov_tool = "genhtml", - test_targets = ["//test/..."], + test_targets = lcov_attrs["test_targets"], )