From d7c0ba3b986cbe5a82a5a10a39ab9f733d410056 Mon Sep 17 00:00:00 2001 From: Dmitri Prime Date: Wed, 20 Nov 2024 15:30:47 -0800 Subject: [PATCH] Add test profiles that disable all `EMBOSS_CHECK`s. (#211) This catches cases where C++ code inadvertently includes side effects inside of an `EMBOSS_CHECK`; the default `EMBOSS_CHECK` uses `assert`, which will be omitted when `NDEBUG` is defined. --- compiler/back_end/cpp/build_defs.bzl | 16 ++++++++++++++-- runtime/cpp/emboss_defines.h | 8 ++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/compiler/back_end/cpp/build_defs.bzl b/compiler/back_end/cpp/build_defs.bzl index a1df7aa..16e0532 100644 --- a/compiler/back_end/cpp/build_defs.bzl +++ b/compiler/back_end/cpp/build_defs.bzl @@ -20,14 +20,26 @@ def emboss_cc_test(name, copts = None, no_w_sign_compare = False, **kwargs): """Generates cc_test rules with and without -DEMBOSS_NO_OPTIMIZATIONS.""" native.cc_test( name = name, - copts = ["-DEMBOSS_FORCE_ALL_CHECKS"] + (copts or []), + copts = copts or [], **kwargs ) native.cc_test( name = name + "_no_opts", copts = [ "-DEMBOSS_NO_OPTIMIZATIONS", - "-DEMBOSS_FORCE_ALL_CHECKS", + ] + ([] if no_w_sign_compare else ["-Wsign-compare"]) + (copts or []), + **kwargs + ) + native.cc_test( + name = name + "_no_checks", + copts = ["-DEMBOSS_SKIP_CHECKS"] + (copts or []), + **kwargs + ) + native.cc_test( + name = name + "_no_checks_no_opts", + copts = [ + "-DEMBOSS_NO_OPTIMIZATIONS", + "-DEMBOSS_SKIP_CHECKS", ] + ([] if no_w_sign_compare else ["-Wsign-compare"]) + (copts or []), **kwargs ) diff --git a/runtime/cpp/emboss_defines.h b/runtime/cpp/emboss_defines.h index 5677874..e247d00 100644 --- a/runtime/cpp/emboss_defines.h +++ b/runtime/cpp/emboss_defines.h @@ -66,9 +66,17 @@ // // By default, checks are only enabled on non-NDEBUG builds. (Note that all // translation units MUST be built with the same value of NDEBUG!) +// +// The EMBOSS_SKIP_CHECKS parameter allows all CHECKs to be compiled out +// without -DNDEBUG; this is useful for testing. #if !defined(EMBOSS_CHECK) +#if defined(EMBOSS_SKIP_CHECKS) +#define EMBOSS_CHECK(x) ((void)0) +#define EMBOSS_CHECK_ABORTS false +#else // !defined(EMBOSS_SKIP_CHECKS) #define EMBOSS_CHECK(x) assert((x)) #define EMBOSS_CHECK_ABORTS (!(NDEBUG)) +#endif // defined(EMBOSS_SKIP_CHECKS) #endif // !defined(EMBOSS_CHECK) #if !defined(EMBOSS_CHECK_ABORTS)