Skip to content

Commit

Permalink
contract: Fix user semicolon enforcement for disabled checks
Browse files Browse the repository at this point in the history
  • Loading branch information
stotko committed May 3, 2020
1 parent 0b9ee8b commit 8e453d2
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/stdgpu/contract.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ namespace stdgpu
* \hideinitializer
* \brief A macro to define in-body conditions for both host and device
*/

//! @cond Doxygen_Suppress
#define STDGPU_DETAIL_EMPTY_STATEMENT (void)0
//! @endcond

#if STDGPU_ENABLE_CONTRACT_CHECKS
#define STDGPU_FORCE_USER_SEMICOLON(statement) do { statement } while (0)

#define STDGPU_DETAIL_HOST_CHECK(type, condition) \
if (!(condition)) \
Expand All @@ -60,18 +64,19 @@ namespace stdgpu
" Condition : %s\n", \
__FILE__, __LINE__, static_cast<const char*>(STDGPU_FUNC), #condition); \
std::terminate(); \
}
} \
STDGPU_DETAIL_EMPTY_STATEMENT

#define STDGPU_DETAIL_HOST_EXPECTS(condition) STDGPU_FORCE_USER_SEMICOLON(STDGPU_DETAIL_HOST_CHECK("Precondition", condition))
#define STDGPU_DETAIL_HOST_ENSURES(condition) STDGPU_FORCE_USER_SEMICOLON(STDGPU_DETAIL_HOST_CHECK("Postcondition", condition))
#define STDGPU_DETAIL_HOST_ASSERT(condition) STDGPU_FORCE_USER_SEMICOLON(STDGPU_DETAIL_HOST_CHECK("Assertion", condition))
#define STDGPU_DETAIL_HOST_EXPECTS(condition) STDGPU_DETAIL_HOST_CHECK("Precondition", condition)
#define STDGPU_DETAIL_HOST_ENSURES(condition) STDGPU_DETAIL_HOST_CHECK("Postcondition", condition)
#define STDGPU_DETAIL_HOST_ASSERT(condition) STDGPU_DETAIL_HOST_CHECK("Assertion", condition)

// FIXME:
// HIP's device assert() function does not seem to override/overload the host compiler version.
// Even using HIP's device assert() function implementation directly results in linker errors.
// Thus, disable contract checks until a better workaround/fix is found.
#if STDGPU_BACKEND == STDGPU_BACKEND_HIP
#define STDGPU_DETAIL_WORKAROUND_ASSERT(condition)
#define STDGPU_DETAIL_WORKAROUND_ASSERT(condition) STDGPU_DETAIL_EMPTY_STATEMENT
#else
#define STDGPU_DETAIL_WORKAROUND_ASSERT(condition) assert(condition)
#endif
Expand All @@ -90,9 +95,9 @@ namespace stdgpu
#define STDGPU_ASSERT(condition) STDGPU_DETAIL_HOST_ASSERT(condition)
#endif
#else
#define STDGPU_EXPECTS(condition)
#define STDGPU_ENSURES(condition)
#define STDGPU_ASSERT(condition)
#define STDGPU_EXPECTS(condition) STDGPU_DETAIL_EMPTY_STATEMENT
#define STDGPU_ENSURES(condition) STDGPU_DETAIL_EMPTY_STATEMENT
#define STDGPU_ASSERT(condition) STDGPU_DETAIL_EMPTY_STATEMENT
#endif

} // namespace stdgpu
Expand Down

0 comments on commit 8e453d2

Please sign in to comment.