Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 20 additions & 53 deletions include/libassert/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
#include <expected>
#endif

#if LIBASSERT_IS_MSVC
#pragma warning(push)
// warning C4251: using non-dll-exported type in dll-exported type, firing on std::vector<frame_ptr> and others for
// some reason
// 4275 is the same thing but for base classes
#pragma warning(disable: 4251; disable: 4275)
#endif
LIBASSERT_WARNING_PRAGMA_PUSH_MSVC

// using non-dll-exported type in dll-exported type, firing on std::vector<frame_ptr> and others for some reason
LIBASSERT_WARNING_PRAGMA_IGNORED_MSVC(4251)

// same thing but for base classes
LIBASSERT_WARNING_PRAGMA_IGNORED_MSVC(4275)

// =====================================================================================================================
// || Libassert public interface ||
Expand Down Expand Up @@ -527,9 +527,7 @@ namespace libassert::detail {
}
}

#if LIBASSERT_IS_MSVC
#pragma warning(pop)
#endif
LIBASSERT_WARNING_PRAGMA_POP_MSVC

#if LIBASSERT_IS_CLANG || LIBASSERT_IS_GCC || !LIBASSERT_NON_CONFORMANT_MSVC_PREPROCESSOR
// Macro mapping utility by William Swanson https://github.com/swansontec/map-macro/blob/master/map.h
Expand Down Expand Up @@ -607,35 +605,6 @@ namespace libassert::detail {
#define LIBASSERT_IF_true(t,...) t
#define LIBASSERT_IF_false(t,f,...) f

#if LIBASSERT_IS_CLANG || LIBASSERT_IS_GCC
#if LIBASSERT_IS_GCC
#define LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_GCC \
_Pragma("GCC diagnostic ignored \"-Wparentheses\"") \
_Pragma("GCC diagnostic ignored \"-Wuseless-cast\"") // #49
#define LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_CLANG
#define LIBASSERT_WARNING_PRAGMA_PUSH_GCC _Pragma("GCC diagnostic push")
#define LIBASSERT_WARNING_PRAGMA_POP_GCC _Pragma("GCC diagnostic pop")
#define LIBASSERT_WARNING_PRAGMA_PUSH_CLANG
#define LIBASSERT_WARNING_PRAGMA_POP_CLANG
#else
#define LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_CLANG \
_Pragma("GCC diagnostic ignored \"-Wparentheses\"") \
_Pragma("GCC diagnostic ignored \"-Woverloaded-shift-op-parentheses\"")
#define LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_GCC
#define LIBASSERT_WARNING_PRAGMA_PUSH_GCC
#define LIBASSERT_WARNING_PRAGMA_POP_GCC
#define LIBASSERT_WARNING_PRAGMA_PUSH_CLANG _Pragma("GCC diagnostic push")
#define LIBASSERT_WARNING_PRAGMA_POP_CLANG _Pragma("GCC diagnostic pop")
#endif
#else
#define LIBASSERT_WARNING_PRAGMA_PUSH_CLANG
#define LIBASSERT_WARNING_PRAGMA_POP_CLANG
#define LIBASSERT_WARNING_PRAGMA_PUSH_GCC
#define LIBASSERT_WARNING_PRAGMA_POP_GCC
#define LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_GCC
#define LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_CLANG
#endif

namespace libassert {
inline void ERROR_ASSERTION_FAILURE_IN_CONSTEXPR_CONTEXT() {
// This non-constexpr method is called from an assertion in a constexpr context if a failure occurs. It is
Expand Down Expand Up @@ -687,9 +656,8 @@ namespace libassert {
}();
#endif

// Note about statement expressions: These are needed for two reasons. The first is putting the arg string array and
// source location structure in .rodata rather than on the stack, the second is a _Pragma for warnings which isn't
// allowed in the middle of an expression by GCC. The semantics are similar to a function return:
// Note about statement expressions: This is needed for putting the arg string array and source location structure in
// .rodata rather than on the stack. The semantics are similar to a function return:
// Given M m; in parent scope, ({ m; }) is an rvalue M&& rather than an lvalue
// ({ M m; m; }) doesn't move, it copies
// ({ M{}; }) does move
Expand All @@ -705,11 +673,6 @@ namespace libassert {
#define LIBASSERT_INVOKE_VAL_PRETTY_FUNCTION_ARG ,libassert::detail::pretty_function_name_wrapper{LIBASSERT_PFUNC}
#endif
#define LIBASSERT_PRETTY_FUNCTION_ARG ,libassert::detail::pretty_function_name_wrapper{LIBASSERT_PFUNC}
#if LIBASSERT_IS_CLANG // -Wall in clang
#define LIBASSERT_IGNORE_UNUSED_VALUE _Pragma("GCC diagnostic ignored \"-Wunused-value\"")
#else
#define LIBASSERT_IGNORE_UNUSED_VALUE
#endif

#define LIBASSERT_BREAKPOINT_IF_DEBUGGING() \
do \
Expand All @@ -729,10 +692,12 @@ namespace libassert {
/* must do awful stuff to workaround differences in where gcc and clang allow these directives to go */ \
do { \
LIBASSERT_WARNING_PRAGMA_PUSH_CLANG \
LIBASSERT_IGNORE_UNUSED_VALUE \
LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_CLANG \
LIBASSERT_WARNING_PRAGMA_IGNORED_CLANG("-Wunused-value") \
LIBASSERT_WARNING_PRAGMA_IGNORED_CLANG("-Wparentheses") \
LIBASSERT_WARNING_PRAGMA_IGNORED_CLANG("-Woverloaded-shift-op-parentheses") \
LIBASSERT_WARNING_PRAGMA_PUSH_GCC \
LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_GCC \
LIBASSERT_WARNING_PRAGMA_IGNORED_GCC("-Wparentheses") \
LIBASSERT_WARNING_PRAGMA_IGNORED_GCC("-Wuseless-cast") \
auto libassert_decomposer = libassert::detail::expression_decomposer( \
libassert::detail::expression_decomposer{} << expr \
); \
Expand Down Expand Up @@ -803,11 +768,13 @@ namespace libassert {
/* must push/pop out here due to nasty clang bug https://github.com/llvm/llvm-project/issues/63897 */ \
/* must do awful stuff to workaround differences in where gcc and clang allow these directives to go */ \
LIBASSERT_WARNING_PRAGMA_PUSH_CLANG \
LIBASSERT_IGNORE_UNUSED_VALUE \
LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_CLANG \
LIBASSERT_WARNING_PRAGMA_IGNORED_CLANG("-Wunused-value") \
LIBASSERT_WARNING_PRAGMA_IGNORED_CLANG("-Wparentheses") \
LIBASSERT_WARNING_PRAGMA_IGNORED_CLANG("-Woverloaded-shift-op-parentheses") \
LIBASSERT_STMTEXPR( \
LIBASSERT_WARNING_PRAGMA_PUSH_GCC \
LIBASSERT_EXPRESSION_DECOMP_WARNING_PRAGMA_GCC \
LIBASSERT_WARNING_PRAGMA_IGNORED_GCC("-Wparentheses") \
LIBASSERT_WARNING_PRAGMA_IGNORED_GCC("-Wuseless-cast") \
auto libassert_decomposer = libassert::detail::expression_decomposer( \
libassert::detail::expression_decomposer{} << expr \
); \
Expand Down
20 changes: 8 additions & 12 deletions include/libassert/expression-decomposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,14 @@ namespace libassert::detail {
LIBASSERT_ATTR_COLD [[nodiscard]]
constexpr decltype(auto) operator()(A&& lhs, B&& rhs) const {
// Go out of the way to support old-style ASSERT(foo && "Message")
#if LIBASSERT_IS_GCC
if constexpr(is_string_literal<B>) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnonnull-compare"
return std::forward<A>(lhs) && std::forward<B>(rhs);
#pragma GCC diagnostic pop
} else {
return std::forward<A>(lhs) && std::forward<B>(rhs);
}
#else
return std::forward<A>(lhs) && std::forward<B>(rhs);
#endif
if constexpr(is_string_literal<B>) {
LIBASSERT_WARNING_PRAGMA_PUSH_GCC
LIBASSERT_WARNING_PRAGMA_IGNORED_GCC("-Wnonnull-compare")
return std::forward<A>(lhs) && std::forward<B>(rhs);
LIBASSERT_WARNING_PRAGMA_POP_GCC
} else {
return std::forward<A>(lhs) && std::forward<B>(rhs);
}
}
};
LIBASSERT_GEN_OP_BOILERPLATE(lor, ||);
Expand Down
42 changes: 25 additions & 17 deletions include/libassert/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,24 +226,32 @@ namespace libassert::detail {
}
}

#if LIBASSERT_IS_CLANG || LIBASSERT_IS_GCC
#if LIBASSERT_IS_GCC
#define LIBASSERT_WARNING_PRAGMA_PUSH_GCC _Pragma("GCC diagnostic push")
#define LIBASSERT_WARNING_PRAGMA_POP_GCC _Pragma("GCC diagnostic pop")
#define LIBASSERT_WARNING_PRAGMA_PUSH_CLANG
#define LIBASSERT_WARNING_PRAGMA_POP_CLANG
#else
#define LIBASSERT_WARNING_PRAGMA_PUSH_GCC
#define LIBASSERT_WARNING_PRAGMA_POP_GCC
#define LIBASSERT_WARNING_PRAGMA_PUSH_CLANG _Pragma("GCC diagnostic push")
#define LIBASSERT_WARNING_PRAGMA_POP_CLANG _Pragma("GCC diagnostic pop")
#endif
#if LIBASSERT_IS_CLANG
#define LIBASSERT_PRAGMA_CLANG(x) _Pragma(#x)
#else
#define LIBASSERT_PRAGMA_CLANG(x)
#endif
#define LIBASSERT_WARNING_PRAGMA_PUSH_CLANG LIBASSERT_PRAGMA_CLANG(GCC diagnostic push)
#define LIBASSERT_WARNING_PRAGMA_POP_CLANG LIBASSERT_PRAGMA_CLANG(GCC diagnostic pop)
#define LIBASSERT_WARNING_PRAGMA_IGNORED_CLANG(x) LIBASSERT_PRAGMA_CLANG(GCC diagnostic ignored #x)

#if LIBASSERT_IS_GCC
#define LIBASSERT_PRAGMA_GCC(x) _Pragma(#x)
#else
#define LIBASSERT_PRAGMA_GCC(x)
#endif
#define LIBASSERT_WARNING_PRAGMA_PUSH_GCC LIBASSERT_PRAGMA_GCC(GCC diagnostic push)
#define LIBASSERT_WARNING_PRAGMA_POP_GCC LIBASSERT_PRAGMA_GCC(GCC diagnostic pop)
#define LIBASSERT_WARNING_PRAGMA_IGNORED_GCC(x) LIBASSERT_PRAGMA_GCC(GCC diagnostic ignored #x)

#if LIBASSERT_IS_MSVC
#define LIBASSERT_PRAGMA_MSVC(x) __pragma(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#else
#define LIBASSERT_WARNING_PRAGMA_PUSH_CLANG
#define LIBASSERT_WARNING_PRAGMA_POP_CLANG
#define LIBASSERT_WARNING_PRAGMA_PUSH_GCC
#define LIBASSERT_WARNING_PRAGMA_POP_GCC
#define LIBASSERT_PRAGMA_MSVC(x)
#endif
#define LIBASSERT_WARNING_PRAGMA_PUSH_MSVC LIBASSERT_PRAGMA_MSVC(warning(push))
#define LIBASSERT_WARNING_PRAGMA_POP_MSVC LIBASSERT_PRAGMA_MSVC(warning(pop))
#define LIBASSERT_WARNING_PRAGMA_IGNORED_MSVC(x) LIBASSERT_PRAGMA_MSVC(warning(disabled: x))

#if LIBASSERT_IS_CLANG || LIBASSERT_IS_ICX
// clang and icx support this as far back as this library could care
Expand All @@ -253,7 +261,7 @@ namespace libassert::detail {
#define LIBASSERT_BREAKPOINT() __debugbreak()
#elif LIBASSERT_IS_GCC
#if LIBASSERT_GCC_VERSION >= 1200
#define LIBASSERT_IGNORE_CPP20_EXTENSION_WARNING _Pragma("GCC diagnostic ignored \"-Wc++20-extensions\"")
#define LIBASSERT_IGNORE_CPP20_EXTENSION_WARNING LIBASSERT_WARNING_PRAGMA_IGNORED_GCC("-Wc++20-extensions")
#else
#define LIBASSERT_IGNORE_CPP20_EXTENSION_WARNING
#endif
Expand Down
10 changes: 3 additions & 7 deletions include/libassert/stringification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,10 @@ namespace libassert::detail {
return "nullptr";
}
}
#if LIBASSERT_IS_GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnonnull"
#endif
LIBASSERT_WARNING_PRAGMA_PUSH_GCC
LIBASSERT_WARNING_PRAGMA_IGNORED_GCC("-Wnonnull")
return stringification::stringify(std::string_view(v));
#if LIBASSERT_IS_GCC
#pragma GCC diagnostic pop
#endif
LIBASSERT_WARNING_PRAGMA_POP_GCC
} else if constexpr(std::is_pointer_v<T> || std::is_function_v<T>) {
return stringification::stringify_pointer_value(reinterpret_cast<const void*>(v));
} else if constexpr(is_smart_pointer<T>) {
Expand Down
6 changes: 2 additions & 4 deletions src/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@
#include "paths.hpp"
#include "printing.hpp"

#if LIBASSERT_IS_MSVC
// wchar -> char string warning
#pragma warning(disable : 4244)
#endif
// wchar -> char string warning
LIBASSERT_WARNING_PRAGMA_IGNORED_MSVC(4244)

using namespace std::string_literals;
using namespace std::string_view_literals;
Expand Down
9 changes: 3 additions & 6 deletions tests/demo/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ int get_mask() {
}

// disable unsafe use of bool warning msvc
#ifdef _MSC_VER
#pragma warning(disable: 4806)
#endif
LIBASSERT_WARNING_PRAGMA_IGNORED_MSVC(4806)

class foo {
public:
Expand Down Expand Up @@ -271,9 +269,8 @@ class foo {
debug_assert((puts("A"), false) && (puts("B"), false));

{
#if defined(__GNUC__) || defined(__GNUG__) // gcc/clang
#pragma GCC diagnostic ignored "-Wshadow"
#endif
LIBASSERT_WARNING_PRAGMA_IGNORED_CLANG("-Wshadow")
LIBASSERT_WARNING_PRAGMA_IGNORED_GCC("-Wshadow")
std::string s = "h1eLlo";
debug_assert(std::find_if(s.begin(), s.end(), [](char c) {
debug_assert(not isdigit(c), c);
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ void recursive_b(int n) {
#define SECTION(s) std::cout<<"===================== ["<<(s)<<"] ====================="<<std::endl;

// disable unsafe use of bool warning msvc
#ifdef _MSC_VER
#pragma warning(disable: 4804)
#endif
LIBASSERT_WARNING_PRAGMA_IGNORED_MSVC(4804)

// TODO: need to check assert, verify, and check...?
// Opt/DNDEBUG
Expand Down
Loading