#include #include #include #include #include #include #include #include #include namespace Void::Build::Report { namespace { using JsonVal = Void::Json::Strict::Value; Void::Result::Error make_error(std::string msg) { return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument, std::move(msg)); } const JsonVal *find_key(const JsonVal::Object &object, std::string_view key) { const auto it = object.find(std::string{key}); return it == object.end() ? nullptr : &it->second; } } // namespace std::vector collect_gcda_files(const std::filesystem::path &root) { std::vector files; std::error_code ec; if (!std::filesystem::is_directory(root, ec)) { return files; } // Per-entry errors (dangling symlinks, unreadable dirs) must not abort the walk: a fresh // build tree can contain them while it is still being populated. try { for (const auto &entry : std::filesystem::recursive_directory_iterator( root, std::filesystem::directory_options::skip_permission_denied)) { std::error_code entry_ec; if (entry.is_regular_file(entry_ec) && !entry_ec && entry.path().extension() == ".gcda") { files.push_back(entry.path()); } } } catch (const std::filesystem::filesystem_error &) { // Keep whatever was collected before the unreadable subtree. } std::ranges::sort(files); return files; } Void::Result::Result write_coverage_report(const std::vector &gcda_files, const std::filesystem::path &output_dir, const std::filesystem::path &source_root, const std::string &gcov_executable) { std::error_code ec; std::filesystem::create_directories(output_dir, ec); if (ec) { return std::unexpected{Void::System::make_system_error( Void::System::map_errno_to_system_code(ec.value()), std::format("cannot create coverage directory '{}': {}", output_dir.string(), ec.message()), ec.value())}; } if (gcda_files.empty()) { return std::unexpected{make_error("no .gcda coverage data was produced")}; } // Source path -> line -> execution count (max across translation units that include it). std::map, std::less<>> coverage; for (const auto &gcda : gcda_files) { Platform::ProcessRequest req; req.executable = gcov_executable; req.arguments = {"--json-format", "--stdout", gcda.filename().string()}; req.working_directory = gcda.parent_path(); req.timeout = std::chrono::seconds{60}; auto res = Platform::run_supervised(req); if (!res || !res->succeeded()) { return std::unexpected{make_error(std::format( "gcov failed for '{}': {}", gcda.string(), res ? res->stderr_data : res.error().message()))}; } auto parsed = Void::Json::Strict::parse(res->stdout_data); if (!parsed) { return std::unexpected{make_error( std::format("gcov produced invalid JSON for '{}'", gcda.string()))}; } const auto *root = std::get_if(&parsed->value); if (root == nullptr) { continue; } std::filesystem::path cwd; if (const auto *cwd_v = find_key(*root, "current_working_directory")) { if (const auto *text = std::get_if(&cwd_v->value)) { cwd = *text; } } const auto *files_v = find_key(*root, "files"); const auto *files = files_v ? std::get_if(&files_v->value) : nullptr; if (files == nullptr) { continue; } for (const auto &file_entry : *files) { const auto *file_obj = std::get_if(&file_entry.value); if (file_obj == nullptr) { continue; } const auto *path_v = find_key(*file_obj, "file"); const auto *path_text = path_v ? std::get_if(&path_v->value) : nullptr; if (path_text == nullptr) { continue; } std::filesystem::path source{*path_text}; if (source.is_relative()) { source = (cwd / source).lexically_normal(); } if (!source_root.empty() && !Platform::path_is_within(source_root, source)) { continue; } const auto *lines_v = find_key(*file_obj, "lines"); const auto *lines = lines_v ? std::get_if(&lines_v->value) : nullptr; if (lines == nullptr) { continue; } auto &entry = coverage[source.string()]; for (const auto &line_entry : *lines) { const auto *line_obj = std::get_if(&line_entry.value); if (line_obj == nullptr) { continue; } std::int64_t number = 0; std::int64_t count = 0; if (const auto *number_v = find_key(*line_obj, "line_number")) { if (const auto *value = std::get_if(&number_v->value)) { number = *value; } } if (const auto *count_v = find_key(*line_obj, "count")) { if (const auto *value = std::get_if(&count_v->value)) { count = *value; } } if (number <= 0) { continue; } auto &slot = entry[number]; slot = std::max(slot, count); } } } CoverageSummary summary; std::string lcov; for (auto &[path, lines] : coverage) { if (lines.empty()) { continue; } ++summary.files; std::size_t hit = 0; lcov += "TN:\nSF:" + path + '\n'; for (const auto &[number, count] : lines) { lcov += std::format("DA:{},{}\n", number, count); if (count > 0) { ++hit; } } lcov += std::format("LF:{}\nLH:{}\nend_of_record\n", lines.size(), hit); summary.lines_found += lines.size(); summary.lines_hit += hit; } if (auto written = Platform::atomic_write_file(output_dir / "coverage.info", lcov, true); !written) { return std::unexpected{written.error()}; } const double percent = summary.lines_found == 0 ? 0.0 : 100.0 * static_cast(summary.lines_hit) / static_cast(summary.lines_found); const std::string summary_text = std::format("Coverage: {:.1f}% ({}/{} lines) across {} file(s)\n", percent, summary.lines_hit, summary.lines_found, summary.files); if (auto written = Platform::atomic_write_file(output_dir / "summary.txt", summary_text, true); !written) { return std::unexpected{written.error()}; } return summary; } } // namespace Void::Build::Report