Stacktrace module

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-04-29 23:23:20 +03:00
1 parent e7186f17f0
commit f0825fbd1a
13 files changed
+105 -36

No files matched your search

+33 -2
View File
@@ -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/<ModuleName>/*.hpp`
@@ -39,7 +41,10 @@ Module layout:
To add a new module, create `src/<ModuleName>/` 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_<UPPERCASE>` 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 <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
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 <reason>` — 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(<target>)`.
### Logging module
`Void::Logging::Logger` is an abstract base class. Derive from it to create a sink:
+22 -1
View File
@@ -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
+20 -1
View File
@@ -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} "$<$<PLATFORM_ID:Windows>:${_lib}>")
endforeach()
foreach(_lib IN LISTS ARG_DEPS_UNIX)
target_link_libraries(${TGT} ${SCOPE} "$<$<NOT:$<PLATFORM_ID:Windows>>:${_lib}>")
endforeach()
if(SRC)
foreach(_opt IN LISTS ARG_OPTIONS_WIN32)
target_compile_options(${TGT} PRIVATE "$<$<PLATFORM_ID:Windows>:${_opt}>")
endforeach()
foreach(_opt IN LISTS ARG_OPTIONS_UNIX)
target_compile_options(${TGT} PRIVATE "$<$<NOT:$<PLATFORM_ID:Windows>>:${_opt}>")
endforeach()
endif()
set_target_properties(${TGT} PROPERTIES
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
@@ -2,7 +2,7 @@
#include <string_view>
#include <string>
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
@@ -3,7 +3,7 @@
#include <vector>
#include <ostream>
namespace Void::Testing::Detail {
namespace Void::Stacktrace {
struct StackFrame {
std::string function_name;
std::string source_file;
@@ -15,4 +15,4 @@ std::vector<StackFrame> 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<StackFrame>& frames);
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames);
} // namespace Void::Testing::Detail
} // namespace Void::Stacktrace
+2 -2
View File
@@ -1,13 +1,13 @@
#pragma once
#include <Void/Testing/Reporter.hpp>
#include <Void/Testing/Detail/Color.hpp>
#include <Void/Stacktrace/Color.hpp>
#include <ostream>
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;
@@ -1,5 +1,5 @@
#include <Void/Testing/Detail/Stacktrace.hpp>
#include <Void/Testing/Detail/Color.hpp>
#include <Void/Stacktrace/Stacktrace.hpp>
#include <Void/Stacktrace/Color.hpp>
#include <fstream>
#include <sstream>
#include <format>
@@ -10,7 +10,7 @@
#include <boost/stacktrace.hpp>
#include <boost/core/demangle.hpp>
namespace Void::Testing::Detail {
namespace Void::Stacktrace {
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames, unsigned max_frames) {
std::vector<StackFrame> result;
@@ -61,4 +61,4 @@ std::string format_stacktrace(const std::vector<StackFrame>& frames) {
}
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames) { os << format_stacktrace(frames); }
} // namespace Void::Testing::Detail
} // namespace Void::Stacktrace
+6
View File
@@ -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
)
-1
View File
@@ -1,5 +1,4 @@
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Detail/Stacktrace.hpp>
namespace Void::Testing::Detail {
void handle_assertion(AssertionResult result) {
auto& ctx = TestContext::current();
+2 -2
View File
@@ -1,12 +1,12 @@
#include <Void/Testing/ConsoleReporter.hpp>
#include <Void/Testing/Detail/Color.hpp>
#include <Void/Stacktrace/Color.hpp>
#include <Void/Testing/Detail/SignalHandler.hpp>
#include <format>
#include <chrono>
#include <algorithm>
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"
+8 -8
View File
@@ -2,10 +2,10 @@
#include "Void/Testing/Config.hpp"
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/ConsoleReporter.hpp>
#include <Void/Testing/Detail/Color.hpp>
#include <Void/Stacktrace/Color.hpp>
#include <Void/Stacktrace/Stacktrace.hpp>
#include <Void/Testing/Detail/Registry.hpp>
#include <Void/Testing/Detail/SignalHandler.hpp>
#include <Void/Testing/Detail/Stacktrace.hpp>
#include <Void/Testing/JUnitReporter.hpp>
#include <algorithm>
#include <csignal>
@@ -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"
+3 -3
View File
@@ -1,5 +1,5 @@
#include <Void/Testing/Detail/SignalHandler.hpp>
#include <Void/Testing/Detail/Stacktrace.hpp>
#include <Void/Stacktrace/Stacktrace.hpp>
#include <csignal>
#include <cstring>
#include <cstdlib>
@@ -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); }
+1 -8
View File
@@ -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)