From 2dd97709a0dca9df2c39e802870aeb6c08fcecff Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Sun, 19 Jun 2022 16:09:39 +0200 Subject: [PATCH] Better unit test response messages when unrecoverable state reached Instead of `exit` we throw `std::runtime_error` in Debug mode assertions and unreachable functions, to have better response from unit test runner. --- Makefile | 2 +- src/errors.cc | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 2d45683..202e4e3 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ MAKEFLAGS="-j $(grep -c ^processor /proc/cpuinfo)" CXXFLAGS:=$(CXXFLAGS) -std=c++20 -Wall -Wextra -Werror=switch -Werror=return-type -Werror=unused-result CPPFLAGS:=$(CPPFLAGS) -Ilib/expected/ -Ilib/ut/ -Ilib/midi/include -Isrc/ -Ilib/bestline/ RELEASE_FLAGS=-O3 -DEBUG_FLAGS=-O0 -ggdb -fsanitize=undefined +DEBUG_FLAGS=-O0 -ggdb -fsanitize=undefined -DDebug CXX=g++ LDFLAGS=-L./lib/midi/ diff --git a/src/errors.cc b/src/errors.cc index 8303c8d..743644d 100644 --- a/src/errors.cc +++ b/src/errors.cc @@ -2,6 +2,7 @@ #include #include +#include template concept Callable = requires(T t, Params ...params) { t(params...); }; @@ -79,6 +80,10 @@ static void encourage_contact(std::ostream &os) void assert(bool condition, std::string message, Location loc) { if (condition) return; +#if Debug + std::cout << "assertion failed at " << loc << " with message: " << message << std::endl; + throw std::runtime_error(message); +#else error_heading(std::cerr, loc, Error_Level::Bug, "Assertion in interpreter"); if (not message.empty()) @@ -87,10 +92,18 @@ void assert(bool condition, std::string message, Location loc) encourage_contact(std::cerr); std::exit(42); +#endif } -[[noreturn]] void unimplemented(std::string_view message, Location loc) +#if !Debug +[[noreturn]] +#endif +void unimplemented(std::string_view message, Location loc) { +#if Debug + std::cout << "unreachable state reached at " << loc << " with message: " << message << std::endl; + throw std::runtime_error(std::string(message)); +#else error_heading(std::cerr, loc, Error_Level::Bug, "This part of interpreter was not implemented yet"); if (not message.empty()) { @@ -100,13 +113,22 @@ void assert(bool condition, std::string message, Location loc) encourage_contact(std::cerr); std::exit(42); +#endif } -[[noreturn]] void unreachable(Location loc) +#if !Debug +[[noreturn]] +#endif +void unreachable(Location loc) { +#if Debug + std::cout << "unreachable state reached at " << loc << std::endl; + throw std::runtime_error("reached unreachable"); +#else error_heading(std::cerr, loc, Error_Level::Bug, "Reached unreachable state"); encourage_contact(std::cerr); std::exit(42); +#endif } std::ostream& operator<<(std::ostream& os, Error const& err)