Skip to content

Commit

Permalink
refactor: add DEBUG_ASSERT to panic.h
Browse files Browse the repository at this point in the history
  • Loading branch information
pflanze committed Sep 9, 2024
1 parent 9a2cb66 commit 7924e82
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ endif ()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address -D DEBUG_ASSERTIONS=1")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -D DEBUG_ASSERTIONS=0")

# Work-around only for MacOS
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_DEBUG "-g -D DEBUG_ASSERTIONS=1")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -D DEBUG_ASSERTIONS=0")
endif ()

set(CMAKE_CXX_STANDARD 20)
Expand Down Expand Up @@ -120,4 +120,4 @@ add_executable(silo_test ${SRC_TEST} $<TARGET_OBJECTS:silolib>)
if (NOT GTest_LIBRARIES)
set(GTest_LIBRARIES gtest gmock)
endif ()
target_link_libraries(silo_test PUBLIC ${GTest_LIBRARIES} silolib)
target_link_libraries(silo_test PUBLIC ${GTest_LIBRARIES} silolib)
35 changes: 34 additions & 1 deletion include/silo/common/panic.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace silo::common {

/// Asserts that the expression `e` evaluates to true. On failure
/// calls `panic` with the stringification of the code `e` and
/// file/line information.
/// file/line information. `ASSERT` is always compiled in; if
/// performance overrides safety, use `DEBUG_ASSERT` instead.
#define ASSERT(e) \
do { \
if (!(e)) { \
Expand All @@ -55,4 +56,36 @@ namespace silo::common {

[[noreturn]] void assertFailure(const char* msg, const char* file, int line);

/// `DEBUG_ASSERT` is like `ASSERT`, but for cases where performance
/// is more important than verification in production: instantiations
/// are only active when compiling SILO in debug (via
/// `CMakeLists.txt`; concretely, they are compiled to be active when
/// the preprocessor variable `DEBUG_ASSERTIONS` is set to 1, and
/// ignored if that variable is set to 0; if the variable is missing,
/// a compilation warning is printed and `DEBUG_ASSERT` is ignored, if
/// present with another value, a compilation error results. Note that
/// `DEBUG_ASSERTIONS` must be set to 1 for debug builds or
/// `DEBUG_ASSERT` won't even check the assertion in debug builds. The
/// SILO `CMakeLists.txt` does set it up that way.)
#ifndef DEBUG_ASSERTIONS
#warning \
"DEBUG_ASSERTIONS is not set, should be 0 to ignore DEBUG_ASSERT, 1 to compile it in, assuming 0"
#define DEBUG_ASSERT(e)
#else // DEBUG_ASSERTIONS is defined
#if DEBUG_ASSERTIONS == 0 /* never */
#define DEBUG_ASSERT(e)
#elif DEBUG_ASSERTIONS == 1 /* always */
#define DEBUG_ASSERT(e) \
do { \
if (!(e)) { \
silo::common::debugAssertFailure(#e, __FILE__, __LINE__); \
} \
} while (0)
#else
#error "DEBUG_ASSERTIONS should be 0 to ignore DEBUG_ASSERT, 1 to compile it in"
#endif
#endif

[[noreturn]] void debugAssertFailure(const char* msg, const char* file, int line);

} // namespace silo::common
4 changes: 4 additions & 0 deletions src/silo/common/panic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ namespace {
panic("ASSERT failure: ", msg, file, line);
}

[[noreturn]] void debugAssertFailure(const char* msg, const char* file, int line) {
panic("DEBUG_ASSERT failure: ", msg, file, line);
}

} // namespace silo::common

0 comments on commit 7924e82

Please sign in to comment.