From 9e663b691b8beb95b5bd61cae1d4b973a4095c32 Mon Sep 17 00:00:00 2001 From: wander Date: Tue, 7 May 2024 20:10:07 +0300 Subject: [PATCH] Added `FLUX_ASSERT` macro --- flux-config/flux/config.hpp | 5 +++- flux-config/flux/config/assert.hpp | 25 ++++++++++++++++++ flux-config/flux/config/features_support.hpp | 26 +++++++++++++++++++ .../flux/logging/detail/source_location.hpp | 1 - 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 flux-config/flux/config/assert.hpp create mode 100644 flux-config/flux/config/features_support.hpp diff --git a/flux-config/flux/config.hpp b/flux-config/flux/config.hpp index df31d00..b497c18 100644 --- a/flux-config/flux/config.hpp +++ b/flux-config/flux/config.hpp @@ -1,3 +1,6 @@ #pragma once -#include \ No newline at end of file +#include + +#include +#include \ No newline at end of file diff --git a/flux-config/flux/config/assert.hpp b/flux-config/flux/config/assert.hpp new file mode 100644 index 0000000..d9ecda2 --- /dev/null +++ b/flux-config/flux/config/assert.hpp @@ -0,0 +1,25 @@ +#pragma once + +// clang-format off +#define FLUX_ASSERT_1_ARGS(expression) \ + (__builtin_expect(static_cast(expression), 1) \ + ? (void)0 \ + : ::flux::io::panic(__FILE__ ":" FLUX_TO_STRING(__LINE__) \ + ": Assertion `" FLUX_TO_STRING(expression) "' failed.")) + +#define FLUX_ASSERT_2_ARGS(expression, message) \ + (__builtin_expect(static_cast(expression), 1) \ + ? (void)0 \ + : ::flux::io::panic(__FILE__ ":" FLUX_TO_STRING(__LINE__) \ + ": Assertion `" FLUX_TO_STRING(expression) "' failed: " message ".")) +// clang-format on + +#define FLUX_ASSERT_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 +#define FLUX_ASSERT_MACRO_CHOOSER(...) \ + FLUX_ASSERT_GET_3RD_ARG(__VA_ARGS__, FLUX_ASSERT_2_ARGS, FLUX_ASSERT_1_ARGS, ) + +#ifdef NDEBUG +# define FLUX_ASSERT(...) ((void)0) +#else +# define FLUX_ASSERT(...) FLUX_ASSERT_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__) +#endif \ No newline at end of file diff --git a/flux-config/flux/config/features_support.hpp b/flux-config/flux/config/features_support.hpp new file mode 100644 index 0000000..5d96d75 --- /dev/null +++ b/flux-config/flux/config/features_support.hpp @@ -0,0 +1,26 @@ +#pragma once + +#if __has_cpp_attribute(msvc::no_unique_address) +// MSVC implements [[no_unique_address]] as a silent no-op currently. If/when MSVC breaks its C++ +// ABI, it will be changed to work as intended. However, MSVC implements [[msvc::no_unique_address]] +// which does what [[no_unique_address]] is supposed to do, in general. + +// Clang-cl does not yet (14.0) implement either [[no_unique_address]] or +// [[msvc::no_unique_address]] though. If/when it does implement [[msvc::no_unique_address]], this +// should be preferred though. +# define FLUX_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] +#elif __has_cpp_attribute(no_unique_address) +# define FLUX_NO_UNIQUE_ADDRESS [[no_unique_address]] +#else +// Note that this can be replaced by #error as soon as clang-cl implements msvc::no_unique_address, +// since there should be no C++20 compiler that doesn't support one of the two attributes at that +// point. I generally don't want to use this macro outside of C++20-only code, because using it +// conditionally in one language version only would make the ABI inconsistent. +# define FLUX_NO_UNIQUE_ADDRESS /* nothing */ +#endif + +#if __has_attribute(__no_sanitize__) +# define FLUX_NO_SANITIZE(...) __attribute__((__no_sanitize__(__VA_ARGS__))) +#else +# define FLUX_NO_SANITIZE(...) +#endif \ No newline at end of file diff --git a/flux-logging/flux/logging/detail/source_location.hpp b/flux-logging/flux/logging/detail/source_location.hpp index 2c4e130..2ac2c5e 100644 --- a/flux-logging/flux/logging/detail/source_location.hpp +++ b/flux-logging/flux/logging/detail/source_location.hpp @@ -78,7 +78,6 @@ print_reserve_define_source_location_impl(char* it, inline constexpr flux_source_location_scatter print_alias_define_source_location_impl(flux::log::source_location location) noexcept { - using flux::log::detail::strip_path; return {{location.file_name(), cstr_len(location.file_name())}, {location.function_name(), cstr_len(location.function_name())}, location.line(),