From f0825fbd1aac9bd22ed2bcef13337d22d1b23c85 Mon Sep 17 00:00:00 2001 From: faumaray Date: Wed, 29 Apr 2026 23:23:20 +0300 Subject: [PATCH] Stacktrace module Signed-off-by: faumaray --- CLAUDE.md | 35 +++++++++++++++++-- cmake/Dependencies.cmake | 23 +++++++++++- cmake/Module.cmake | 21 ++++++++++- .../{Testing/Detail => Stacktrace}/Color.hpp | 4 +-- .../Detail => Stacktrace}/Stacktrace.hpp | 4 +-- include/Void/Testing/ConsoleReporter.hpp | 4 +-- src/{Testing => Stacktrace}/Stacktrace.cpp | 8 ++--- src/Stacktrace/module.cmake | 6 ++++ src/Testing/Assert.cpp | 1 - src/Testing/ConsoleReporter.cpp | 4 +-- src/Testing/Runner.cpp | 16 ++++----- src/Testing/SignalHandler.cpp | 6 ++-- src/Testing/module.cmake | 9 +---- 13 files changed, 105 insertions(+), 36 deletions(-) rename include/Void/{Testing/Detail => Stacktrace}/Color.hpp (95%) rename include/Void/{Testing/Detail => Stacktrace}/Stacktrace.hpp (88%) rename src/{Testing => Stacktrace}/Stacktrace.cpp (94%) create mode 100644 src/Stacktrace/module.cmake diff --git a/CLAUDE.md b/CLAUDE.md index b07b67e..20fe135 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -4,6 +4,8 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ## Build +Requires CMake 4.0+ and a C++23 compiler (clang++ is the default in the configure command below). + ```bash # Configure (clang++, Ninja, Debug) cmake -B build -S . -G Ninja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Debug @@ -29,7 +31,7 @@ VoidLibrary is a C++23 modular shared library. Each module is an independently i ### Module system -`cmake/Module.cmake` defines `void_add_module(NAME ...)`. The root `CMakeLists.txt` auto-discovers every subdirectory under `src/` and calls it. A module with only headers becomes an `INTERFACE` library; one with `.cpp` sources becomes a `SHARED` library. +`cmake/Module.cmake` defines `void_add_module(NAME ...)`. The root `CMakeLists.txt` auto-discovers every subdirectory under `src/` and calls it. A module with only headers becomes an `INTERFACE` library; one with `.cpp` sources becomes `SHARED` — except `Testing`, which is special-cased to `STATIC`. Module layout: - Headers: `include/Void//*.hpp` @@ -39,7 +41,10 @@ Module layout: To add a new module, create `src//` and populate it — the build system picks it up automatically. If a module needs special dependency wiring, drop a `module.cmake` file inside the source directory (see `cmake/module.cmake.template` for the pattern). -External dependencies are fetched via `cmake/Dependencies.cmake` using `FetchContent`; add new ones there and reference them via `EXTERNAL_DEPS` in `void_add_module`. +`void_add_module` accepts three dependency lists: +- `INTERNAL_DEPS` — other `Void::*` modules (linked unconditionally). +- `EXTERNAL_DEPS` — third-party targets fetched via `cmake/Dependencies.cmake` (`FetchContent`); add new ones there before referencing. +- `OPTIONAL_DEPS` — `Void::*` modules linked only if they exist; each present dep additionally defines `VOID_HAS_` for conditional code. ### Tests @@ -52,6 +57,32 @@ External dependencies are fetched via `cmake/Dependencies.cmake` using `FetchCon Standalone tests are preferred for testing a single module in isolation. +#### Writing a test + +Tests are free functions (or methods on a `Void::Testing::Fixture` subclass) returning `Void::Testing::Test` or `Void::Testing::DisabledTest` (both aliases for `void`). **Never write the registration code yourself** — `cmake/VoidTestScanner.cmake` parses each `.cpp` at configure time, generates `*.void_tests.cpp` shims with `Void::Testing::Detail::test_registrar` calls, and rewrites the original source to `HEADER_FILE_ONLY`. + +```cpp +#include +#include + +Void::Testing::Test addition() { Void::Testing::expect_eq(1 + 1, 2); } + +// @tag integration, database +Void::Testing::Test db_query() { Void::Testing::expect(true); } + +// @skip "Feature #42 not implemented" +Void::Testing::DisabledTest broken() { Void::Testing::fail("not ready"); } +``` + +Fixture-based tests work the same way — declare a class derived from `Void::Testing::Fixture`, override `setup()`/`teardown()`, and write methods returning `Test`/`DisabledTest`. + +Comment metadata (immediately preceding the function): +- `// @tag a, b, c` — attaches tags. +- `// @skip ` — marks the test skipped with a reason; quoted or unquoted strings both work. `DisabledTest` implies `@skip "disabled"` if no explicit reason is given. +- Any non-comment, non-empty line resets the accumulated tags/skip — keep the comment block adjacent to the function. + +The sweep in `cmake/VoidTesting.cmake` auto-applies the scan to every executable that transitively links `Void::Testing`. To opt a target out, set the `VOID_TESTING_NO_AUTO_SCAN` property; to opt in explicitly (e.g. for a target that doesn't link `Void::Testing` directly), call `void_add_tests()`. + ### Logging module `Void::Logging::Logger` is an abstract base class. Derive from it to create a sink: diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index d0abe87..70ec163 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -2,7 +2,28 @@ include(FetchContent) function(void_fetch_dependency DEP_NAME) if(DEP_NAME STREQUAL "Boost") - find_package(Boost REQUIRED COMPONENTS stacktrace_backtrace) + if(TARGET Boost::stacktrace_backtrace) + return() + endif() + + find_package(Boost QUIET COMPONENTS stacktrace_backtrace) + if(TARGET Boost::stacktrace_backtrace) + return() + endif() + + message(STATUS "[Void] Boost::stacktrace_backtrace not found — falling back to FetchContent") + set(BOOST_INCLUDE_LIBRARIES stacktrace CACHE STRING "" FORCE) + set(BOOST_ENABLE_CMAKE ON CACHE BOOL "" FORCE) + FetchContent_Declare(Boost + URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz + URL_HASH SHA256=2e64e5d79a738d0fa6fb546c6e5c2bd28f88d268a2a080546f74e5ff98f29d0e + EXCLUDE_FROM_ALL + ) + FetchContent_MakeAvailable(Boost) + + if(NOT TARGET Boost::stacktrace_backtrace AND TARGET boost_stacktrace_backtrace) + add_library(Boost::stacktrace_backtrace ALIAS boost_stacktrace_backtrace) + endif() return() endif() # FIXME: Add if needed external diff --git a/cmake/Module.cmake b/cmake/Module.cmake index d893bc1..00b37f6 100644 --- a/cmake/Module.cmake +++ b/cmake/Module.cmake @@ -2,7 +2,9 @@ set_property(GLOBAL PROPERTY VOID_MODULES "") set_property(GLOBAL PROPERTY VOID_EXT_DEPS "") function(void_add_module MODULE_NAME) - cmake_parse_arguments(ARG "" "" "INTERNAL_DEPS;EXTERNAL_DEPS;OPTIONAL_DEPS" ${ARGN}) + cmake_parse_arguments(ARG "" "" + "INTERNAL_DEPS;EXTERNAL_DEPS;OPTIONAL_DEPS;DEPS_WIN32;DEPS_UNIX;OPTIONS_WIN32;OPTIONS_UNIX" + ${ARGN}) set(TGT ${PROJECT_NAME}_${MODULE_NAME}) set(ALIAS ${PROJECT_NAME}::${MODULE_NAME}) @@ -56,6 +58,23 @@ function(void_add_module MODULE_NAME) if(ARG_EXTERNAL_DEPS) target_link_libraries(${TGT} ${SCOPE} ${ARG_EXTERNAL_DEPS}) endif() + + foreach(_lib IN LISTS ARG_DEPS_WIN32) + target_link_libraries(${TGT} ${SCOPE} "$<$:${_lib}>") + endforeach() + foreach(_lib IN LISTS ARG_DEPS_UNIX) + target_link_libraries(${TGT} ${SCOPE} "$<$>:${_lib}>") + endforeach() + + if(SRC) + foreach(_opt IN LISTS ARG_OPTIONS_WIN32) + target_compile_options(${TGT} PRIVATE "$<$:${_opt}>") + endforeach() + foreach(_opt IN LISTS ARG_OPTIONS_UNIX) + target_compile_options(${TGT} PRIVATE "$<$>:${_opt}>") + endforeach() + endif() + set_target_properties(${TGT} PROPERTIES CXX_STANDARD 23 CXX_STANDARD_REQUIRED ON diff --git a/include/Void/Testing/Detail/Color.hpp b/include/Void/Stacktrace/Color.hpp similarity index 95% rename from include/Void/Testing/Detail/Color.hpp rename to include/Void/Stacktrace/Color.hpp index 735d31f..6a03b71 100644 --- a/include/Void/Testing/Detail/Color.hpp +++ b/include/Void/Stacktrace/Color.hpp @@ -2,7 +2,7 @@ #include #include -namespace Void::Testing::Detail::Color { +namespace Void::Stacktrace::Color { inline bool g_color_enabled{true}; constexpr std::string_view reset = "\033[0m"; @@ -31,4 +31,4 @@ inline std::string info(std::string_view text) { return apply(bold_cyan, tex inline std::string hint(std::string_view text) { return apply(dim, text); } inline std::string heading(std::string_view text) { return apply(bold_blue, text); } inline std::string emphasis(std::string_view text) { return apply(bold, text); } -} // namespace Void::Testing::Detail::Color +} // namespace Void::Stacktrace::Color diff --git a/include/Void/Testing/Detail/Stacktrace.hpp b/include/Void/Stacktrace/Stacktrace.hpp similarity index 88% rename from include/Void/Testing/Detail/Stacktrace.hpp rename to include/Void/Stacktrace/Stacktrace.hpp index ba55890..e708a82 100644 --- a/include/Void/Testing/Detail/Stacktrace.hpp +++ b/include/Void/Stacktrace/Stacktrace.hpp @@ -3,7 +3,7 @@ #include #include -namespace Void::Testing::Detail { +namespace Void::Stacktrace { struct StackFrame { std::string function_name; std::string source_file; @@ -15,4 +15,4 @@ std::vector capture_stacktrace(unsigned skip_frames = 0, unsigned ma std::string read_source_snippet(const std::string& file, int line, int context_lines = 3); std::string format_stacktrace(const std::vector& frames); void print_stacktrace(std::ostream& os, const std::vector& frames); -} // namespace Void::Testing::Detail +} // namespace Void::Stacktrace diff --git a/include/Void/Testing/ConsoleReporter.hpp b/include/Void/Testing/ConsoleReporter.hpp index 41de3ca..fa816b4 100644 --- a/include/Void/Testing/ConsoleReporter.hpp +++ b/include/Void/Testing/ConsoleReporter.hpp @@ -1,13 +1,13 @@ #pragma once #include -#include +#include #include namespace Void::Testing { class ConsoleReporter final : public Reporter { public: explicit ConsoleReporter(std::ostream& os, bool color_enabled = true) : os_(os) { - Detail::Color::g_color_enabled = color_enabled; + Void::Stacktrace::Color::g_color_enabled = color_enabled; } void on_run_start(const RunConfig& config, int total_tests) override; void on_suite_start(const std::string& suite_name, int test_count) override; diff --git a/src/Testing/Stacktrace.cpp b/src/Stacktrace/Stacktrace.cpp similarity index 94% rename from src/Testing/Stacktrace.cpp rename to src/Stacktrace/Stacktrace.cpp index b4b8f6a..e1d1009 100644 --- a/src/Testing/Stacktrace.cpp +++ b/src/Stacktrace/Stacktrace.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include @@ -10,7 +10,7 @@ #include #include -namespace Void::Testing::Detail { +namespace Void::Stacktrace { std::vector capture_stacktrace(unsigned skip_frames, unsigned max_frames) { std::vector result; @@ -61,4 +61,4 @@ std::string format_stacktrace(const std::vector& frames) { } void print_stacktrace(std::ostream& os, const std::vector& frames) { os << format_stacktrace(frames); } -} // namespace Void::Testing::Detail +} // namespace Void::Stacktrace diff --git a/src/Stacktrace/module.cmake b/src/Stacktrace/module.cmake new file mode 100644 index 0000000..c8fa330 --- /dev/null +++ b/src/Stacktrace/module.cmake @@ -0,0 +1,6 @@ +void_add_module(Stacktrace + EXTERNAL_DEPS Boost::stacktrace_backtrace + DEPS_WIN32 dbghelp + DEPS_UNIX dl + OPTIONS_UNIX -g -fno-omit-frame-pointer +) diff --git a/src/Testing/Assert.cpp b/src/Testing/Assert.cpp index 279cf65..b4f7214 100644 --- a/src/Testing/Assert.cpp +++ b/src/Testing/Assert.cpp @@ -1,5 +1,4 @@ #include -#include namespace Void::Testing::Detail { void handle_assertion(AssertionResult result) { auto& ctx = TestContext::current(); diff --git a/src/Testing/ConsoleReporter.cpp b/src/Testing/ConsoleReporter.cpp index e7d327f..c4496cb 100644 --- a/src/Testing/ConsoleReporter.cpp +++ b/src/Testing/ConsoleReporter.cpp @@ -1,12 +1,12 @@ #include -#include +#include #include #include #include #include namespace Void::Testing { -using namespace Detail::Color; +using namespace Void::Stacktrace::Color; void ConsoleReporter::on_run_start(const RunConfig&, int total_tests) { os_ << "\n" << apply(bold_blue, "╭──────────────────────────────────────────────╮") << "\n" diff --git a/src/Testing/Runner.cpp b/src/Testing/Runner.cpp index 4f8f525..90b36d5 100644 --- a/src/Testing/Runner.cpp +++ b/src/Testing/Runner.cpp @@ -2,10 +2,10 @@ #include "Void/Testing/Config.hpp" #include #include -#include +#include +#include #include #include -#include #include #include #include @@ -161,13 +161,13 @@ namespace Void::Testing result.state = TestState::Failed; result.crash_info = std::format("Unhandled exception: {}", e.what()); - result.stacktrace = - Detail::format_stacktrace(Detail::capture_stacktrace(1, 8)); + result.stacktrace = Void::Stacktrace::format_stacktrace( + Void::Stacktrace::capture_stacktrace(1, 8)); } catch (...) { result.state = TestState::Failed; result.crash_info = "Unhandled unknown exception"; - result.stacktrace = - Detail::format_stacktrace(Detail::capture_stacktrace(1, 8)); + result.stacktrace = Void::Stacktrace::format_stacktrace( + Void::Stacktrace::capture_stacktrace(1, 8)); } auto end = std::chrono::high_resolution_clock::now(); @@ -209,9 +209,9 @@ namespace Void::Testing RunResult Runner::run(const RunConfig& config) { - Detail::Color::g_color_enabled = !config.no_color; + Void::Stacktrace::Color::g_color_enabled = !config.no_color; if (config.show_help) { - using namespace Detail::Color; + using namespace Void::Stacktrace::Color; std::cout << "\n" diff --git a/src/Testing/SignalHandler.cpp b/src/Testing/SignalHandler.cpp index d05cbd7..2a1bfdf 100644 --- a/src/Testing/SignalHandler.cpp +++ b/src/Testing/SignalHandler.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -30,8 +30,8 @@ const char* signal_name(int sig) { static void crash_handler(int sig) { g_last_signal = sig; - auto frames = capture_stacktrace(1, 16); - auto trace = format_stacktrace(frames); + auto frames = Void::Stacktrace::capture_stacktrace(1, 16); + auto trace = Void::Stacktrace::format_stacktrace(frames); if (g_crash_callback) g_crash_callback(sig, trace); #ifndef _WIN32 if (g_jump_ready) { g_jump_ready = false; siglongjmp(g_jump_buffer, sig); } diff --git a/src/Testing/module.cmake b/src/Testing/module.cmake index c94734e..ba72fd2 100644 --- a/src/Testing/module.cmake +++ b/src/Testing/module.cmake @@ -1,8 +1 @@ -void_add_module(Testing EXTERNAL_DEPS Boost::stacktrace_backtrace) - -if(WIN32) - target_link_libraries(Void_Testing PUBLIC dbghelp) -else() - target_link_libraries(Void_Testing PUBLIC dl) - target_compile_options(Void_Testing PRIVATE -g -fno-omit-frame-pointer) -endif() +void_add_module(Testing INTERNAL_DEPS Stacktrace)