From 7233c877f932b13e55cdcadbcd5e41c3a5302aaf Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Mon, 6 May 2024 19:54:38 -0500 Subject: [PATCH] A handful of code consistency fixes --- include/libassert/assert.hpp | 4 +- include/libassert/platform.hpp | 163 +++++++++++++++------------------ 2 files changed, 73 insertions(+), 94 deletions(-) diff --git a/include/libassert/assert.hpp b/include/libassert/assert.hpp index 60b8fadf..60fdfa3f 100644 --- a/include/libassert/assert.hpp +++ b/include/libassert/assert.hpp @@ -23,7 +23,7 @@ #include #endif -#include "libassert/platform.hpp" +#include #if LIBASSERT_STD_VER >= 20 #include @@ -52,7 +52,6 @@ // Block comments are used to create some visual separation and try to break the library into more manageable parts. // I've tried as much as I can to keep logically connected parts together but there is some bootstrapping necessary. - // always_false is just convenient to use here #define LIBASSERT_PHONY_USE(E) ((void)libassert::detail::always_false) @@ -64,7 +63,6 @@ #pragma warning(disable: 4251; disable: 4275) #endif - // ===================================================================================================================== // || Core utilities || // ===================================================================================================================== diff --git a/include/libassert/platform.hpp b/include/libassert/platform.hpp index 420cde25..1cbe8728 100644 --- a/include/libassert/platform.hpp +++ b/include/libassert/platform.hpp @@ -4,23 +4,19 @@ // Copyright (c) 2021-2024 Jeremy Rifkin under the MIT license // https://github.com/jeremy-rifkin/libassert - - // ===================================================================================================================== // || Preprocessor stuff || // ===================================================================================================================== // Set the C++ version number based on if we are on a dumb compiler like MSVC or not. #ifdef _MSVC_LANG - #define LIBASSERT_CPLUSPLUS _MSVC_LANG + #define LIBASSERT_CPLUSPLUS _MSVC_LANG #else - #define LIBASSERT_CPLUSPLUS __cplusplus + #define LIBASSERT_CPLUSPLUS __cplusplus #endif - -// Validate that we are using a C++17 or newer compiler. #if LIBASSERT_CPLUSPLUS < 201703L - #error "libassert requires C++17 or newer" + #error "libassert requires C++17 or newer" #endif #ifndef LIBASSERT_STD_VER @@ -35,145 +31,136 @@ #endif #endif - /// /// Detect compiler versions. /// - #if defined(__clang__) && !defined(__ibmxl__) - #define LIBASSERT_IS_CLANG 1 - #define LIBASSERT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) + #define LIBASSERT_IS_CLANG 1 + #define LIBASSERT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) #else - #define LIBASSERT_IS_CLANG 0 - #define LIBASSERT_CLANG_VERSION 0 + #define LIBASSERT_IS_CLANG 0 + #define LIBASSERT_CLANG_VERSION 0 #endif #if (defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__) && !defined(__INTEL_COMPILER) - #define LIBASSERT_IS_GCC 1 - #define LIBASSERT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) + #define LIBASSERT_IS_GCC 1 + #define LIBASSERT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) #else - #define LIBASSERT_IS_GCC 0 - #define LIBASSERT_GCC_VERSION 0 + #define LIBASSERT_IS_GCC 0 + #define LIBASSERT_GCC_VERSION 0 #endif -#if defined(_MSC_VER) - #define LIBASSERT_IS_MSVC 1 - #define LIBASSERT_MSVC_VERSION _MSC_VER - #include // alternative operator tokens are standard but msvc requires the include or /permissive- or /Za +#ifdef _MSC_VER + #define LIBASSERT_IS_MSVC 1 + #define LIBASSERT_MSVC_VERSION _MSC_VER #else - #define LIBASSERT_IS_MSVC 0 - #define LIBASSERT_MSVC_VERSION 0 + #define LIBASSERT_IS_MSVC 0 + #define LIBASSERT_MSVC_VERSION 0 #endif - /// /// Detect standard library versions. /// // libstdc++ #ifdef _GLIBCXX_RELEASE - #define LIBASSERT_GLIBCXX_RELEASE _GLIBCXX_RELEASE + #define LIBASSERT_GLIBCXX_RELEASE _GLIBCXX_RELEASE #else - #define LIBASSERT_GLIBCXX_RELEASE 0 + #define LIBASSERT_GLIBCXX_RELEASE 0 #endif - #ifdef _LIBCPP_VERSION - #define LIBASSERT_LIBCPP_VERSION _LIBCPP_VERSION + #define LIBASSERT_LIBCPP_VERSION _LIBCPP_VERSION #else - #define LIBASSERT_LIBCPP_VERSION 0 + #define LIBASSERT_LIBCPP_VERSION 0 #endif - /// /// Helper macros for compiler attributes. /// #ifdef __has_cpp_attribute - #define LIBASSERT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) + #define LIBASSERT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) #else - #define LIBASSERT_HAS_CPP_ATTRIBUTE(x) 0 + #define LIBASSERT_HAS_CPP_ATTRIBUTE(x) 0 #endif - /// /// Compiler attribute support. /// #if LIBASSERT_HAS_CPP_ATTRIBUTE(nodiscard) && LIBASSERT_STD_VER >= 20 - #define LIBASSERT_ATTR_NODISCARD_MSG(msg) [[nodiscard(msg)]] + #define LIBASSERT_ATTR_NODISCARD_MSG(msg) [[nodiscard(msg)]] #else // Assume we have normal C++17 nodiscard support. - #define LIBASSERT_ATTR_NODISCARD_MSG(msg) [[nodiscard]] + #define LIBASSERT_ATTR_NODISCARD_MSG(msg) [[nodiscard]] #endif #if LIBASSERT_HAS_CPP_ATTRIBUTE(no_unique_address) - #define LIBASSERT_ATTR_NO_UNIQUE_ADDRESS [[no_unique_address]] + #define LIBASSERT_ATTR_NO_UNIQUE_ADDRESS [[no_unique_address]] #else - #define LIBASSERT_ATTR_NO_UNIQUE_ADDRESS + #define LIBASSERT_ATTR_NO_UNIQUE_ADDRESS #endif - /// /// General project macros /// - #ifdef _WIN32 - #define LIBASSERT_EXPORT_ATTR __declspec(dllexport) - #define LIBASSERT_IMPORT_ATTR __declspec(dllimport) + #define LIBASSERT_EXPORT_ATTR __declspec(dllexport) + #define LIBASSERT_IMPORT_ATTR __declspec(dllimport) #else - #define LIBASSERT_EXPORT_ATTR __attribute__((visibility("default"))) - #define LIBASSERT_IMPORT_ATTR __attribute__((visibility("default"))) + #define LIBASSERT_EXPORT_ATTR __attribute__((visibility("default"))) + #define LIBASSERT_IMPORT_ATTR __attribute__((visibility("default"))) #endif #ifdef LIBASSERT_STATIC_DEFINE - #define LIBASSERT_EXPORT - #define LIBASSERT_NO_EXPORT + #define LIBASSERT_EXPORT + #define LIBASSERT_NO_EXPORT #else - #ifndef LIBASSERT_EXPORT - #ifdef libassert_lib_EXPORTS - /* We are building this library */ - #define LIBASSERT_EXPORT LIBASSERT_EXPORT_ATTR - #else - /* We are using this library */ - #define LIBASSERT_EXPORT LIBASSERT_IMPORT_ATTR - #endif - #endif + #ifndef LIBASSERT_EXPORT + #ifdef libassert_lib_EXPORTS + /* We are building this library */ + #define LIBASSERT_EXPORT LIBASSERT_EXPORT_ATTR + #else + /* We are using this library */ + #define LIBASSERT_EXPORT LIBASSERT_IMPORT_ATTR + #endif + #endif #endif #if LIBASSERT_IS_CLANG || LIBASSERT_IS_GCC - #define LIBASSERT_PFUNC __extension__ __PRETTY_FUNCTION__ - #define LIBASSERT_ATTR_COLD [[gnu::cold]] - #define LIBASSERT_ATTR_NOINLINE [[gnu::noinline]] - #define LIBASSERT_UNREACHABLE_CALL __builtin_unreachable() + #define LIBASSERT_PFUNC __extension__ __PRETTY_FUNCTION__ + #define LIBASSERT_ATTR_COLD [[gnu::cold]] + #define LIBASSERT_ATTR_NOINLINE [[gnu::noinline]] + #define LIBASSERT_UNREACHABLE_CALL __builtin_unreachable() #else - #define LIBASSERT_PFUNC __FUNCSIG__ - #define LIBASSERT_ATTR_COLD - #define LIBASSERT_ATTR_NOINLINE __declspec(noinline) - #define LIBASSERT_UNREACHABLE_CALL __assume(false) + #define LIBASSERT_PFUNC __FUNCSIG__ + #define LIBASSERT_ATTR_COLD + #define LIBASSERT_ATTR_NOINLINE __declspec(noinline) + #define LIBASSERT_UNREACHABLE_CALL __assume(false) #endif #if LIBASSERT_IS_MSVC - #define LIBASSERT_STRONG_EXPECT(expr, value) (expr) + #define LIBASSERT_STRONG_EXPECT(expr, value) (expr) #elif defined(__clang__) && __clang_major__ >= 11 || __GNUC__ >= 9 - #define LIBASSERT_STRONG_EXPECT(expr, value) __builtin_expect_with_probability((expr), (value), 1) + #define LIBASSERT_STRONG_EXPECT(expr, value) __builtin_expect_with_probability((expr), (value), 1) #else - #define LIBASSERT_STRONG_EXPECT(expr, value) __builtin_expect((expr), (value)) + #define LIBASSERT_STRONG_EXPECT(expr, value) __builtin_expect((expr), (value)) #endif // deal with gcc shenanigans // at one point their std::string's move assignment was not noexcept even in c++17 // https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html #if defined(_GLIBCXX_USE_CXX11_ABI) - // NOLINTNEXTLINE(misc-include-cleaner) - #define LIBASSERT_GCC_ISNT_STUPID _GLIBCXX_USE_CXX11_ABI + // NOLINTNEXTLINE(misc-include-cleaner) + #define LIBASSERT_GCC_ISNT_STUPID _GLIBCXX_USE_CXX11_ABI #else - // assume others target new abi by default - homework - #define LIBASSERT_GCC_ISNT_STUPID 1 + // assume others target new abi by default - homework + #define LIBASSERT_GCC_ISNT_STUPID 1 #endif #if defined(_MSVC_TRADITIONAL) && _MSVC_TRADITIONAL @@ -190,55 +177,49 @@ #define LIBASSERT_VA_ARGS(...) , ##__VA_ARGS__ #endif - /// /// C++20 functionality wrappers. /// // Check if we can use std::is_constant_evaluated. #ifdef __has_include - #if __has_include() - #include - #ifdef __cpp_lib_is_constant_evaluated - #include - #define LIBASSERT_HAS_IS_CONSTANT_EVALUATED - #endif - #endif + #if __has_include() + #include + #ifdef __cpp_lib_is_constant_evaluated + #include + #define LIBASSERT_HAS_IS_CONSTANT_EVALUATED + #endif + #endif #endif - // Check if we have the builtin __builtin_is_constant_evaluated. #ifdef __has_builtin - #if __has_builtin(__builtin_is_constant_evaluated) - #define LIBASSERT_HAS_BUILTIN_IS_CONSTANT_EVALUATED - #endif + #if __has_builtin(__builtin_is_constant_evaluated) + #define LIBASSERT_HAS_BUILTIN_IS_CONSTANT_EVALUATED + #endif #endif - // GCC 9.1+ and later has __builtin_is_constant_evaluated #if (__GNUC__ >= 9) && !defined(LIBASSERT_HAS_BUILTIN_IS_CONSTANT_EVALUATED) - #define LIBASSERT_HAS_BUILTIN_IS_CONSTANT_EVALUATED + #define LIBASSERT_HAS_BUILTIN_IS_CONSTANT_EVALUATED #endif - // Visual Studio 2019 (19.25) and later supports __builtin_is_constant_evaluated #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 192528326) - #define LIBASSERT_HAS_BUILTIN_IS_CONSTANT_EVALUATED + #define LIBASSERT_HAS_BUILTIN_IS_CONSTANT_EVALUATED #endif - namespace libassert::support { // Note: Works with >=C++20 and with C++17 for GCC 9.1+, Clang 9+, and MSVC 19.25+. constexpr bool is_constant_evaluated() noexcept { #if defined(LIBASSERT_HAS_IS_CONSTANT_EVALUATED) - return std::is_constant_evaluated(); + return std::is_constant_evaluated(); #elif defined(LIBASSERT_HAS_BUILTIN_IS_CONSTANT_EVALUATED) - return __builtin_is_constant_evaluated(); + return __builtin_is_constant_evaluated(); #else - return false; + return false; #endif } } - -#endif // LIBASSERT_PLATFORM_HPP +#endif