From f169d37074554a24966437ab53af0e4ee09372fc Mon Sep 17 00:00:00 2001 From: lyken Date: Thu, 15 Aug 2024 22:07:38 +0800 Subject: [PATCH] core/irrt/exceptions: add debug utils with exceptions --- nac3core/build.rs | 18 ++++++++++++------ nac3core/irrt/irrt/debug.hpp | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 nac3core/irrt/irrt/debug.hpp diff --git a/nac3core/build.rs b/nac3core/build.rs index cd14b0cb..b56803dd 100644 --- a/nac3core/build.rs +++ b/nac3core/build.rs @@ -18,18 +18,13 @@ fn main() { * HACK: Sadly, clang doesn't let us emit generic LLVM bitcode. * Compiling for WASM32 and filtering the output with regex is the closest we can get. */ - let flags: &[&str] = &[ + let mut flags: Vec<&str> = vec![ "--target=wasm32", "-x", "c++", "-fno-discard-value-names", "-fno-exceptions", "-fno-rtti", - match env::var("PROFILE").as_deref() { - Ok("debug") => "-O0", - Ok("release") => "-O3", - flavor => panic!("Unknown or missing build flavor {flavor:?}"), - }, "-emit-llvm", "-S", "-Wall", @@ -41,6 +36,17 @@ fn main() { irrt_cpp_path.to_str().unwrap(), ]; + match env::var("PROFILE").as_deref() { + Ok("debug") => { + flags.push("-O0"); + flags.push("-DIRRT_DEBUG_ASSERT"); + } + Ok("release") => { + flags.push("-O3"); + } + flavor => panic!("Unknown or missing build flavor {flavor:?}"), + } + // Tell Cargo to rerun if any file under `irrt_dir` (recursive) changes println!("cargo:rerun-if-changed={}", irrt_dir.to_str().unwrap()); diff --git a/nac3core/irrt/irrt/debug.hpp b/nac3core/irrt/irrt/debug.hpp new file mode 100644 index 00000000..f5baea7a --- /dev/null +++ b/nac3core/irrt/irrt/debug.hpp @@ -0,0 +1,23 @@ +#pragma once + +// Set in nac3core/build.rs +#ifdef IRRT_DEBUG_ASSERT +#define IRRT_DEBUG_ASSERT_BOOL true +#else +#define IRRT_DEBUG_ASSERT_BOOL false +#endif + +#define raise_debug_assert(SizeT, msg, param1, param2, param3) \ + raise_exception(SizeT, EXN_ASSERTION_ERROR, "IRRT debug assert failed: " msg, param1, param2, param3); + +#define debug_assert_eq(SizeT, lhs, rhs) \ + if (IRRT_DEBUG_ASSERT_BOOL && (lhs) != (rhs)) \ + { \ + raise_debug_assert(SizeT, "LHS = {0}. RHS = {1}", lhs, rhs, NO_PARAM); \ + } + +#define debug_assert(SizeT, expr) \ + if (IRRT_DEBUG_ASSERT_BOOL && !(expr)) \ + { \ + raise_debug_assert(SizeT, "Got false.", NO_PARAM, NO_PARAM, NO_PARAM); \ + } \ No newline at end of file