preCICE v3.1.1
Loading...
Searching...
No Matches
assertion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdlib>
4#include <iostream>
5#include <string_view>
6
7#include "utils/fmt.hpp"
8
9// Assertions are disabled in release (NDEBUG) builds by default.
10// To enable them anyhow, enable the CMake option PRECICE_RELEASE_WITH_ASSERTIONS.
11
12#if defined(NDEBUG) && !defined(PRECICE_RELEASE_WITH_ASSERTIONS)
13#define PRECICE_NO_ASSERTIONS
14#endif
15
16#ifdef PRECICE_NO_ASSERTIONS
17
18#include "utils/ignore.hpp"
19
20#define PRECICE_ASSERT(...) \
21 ::precice::utils::ignore(__VA_ARGS__)
22
23#else
24
25#include <boost/current_function.hpp>
26#include <boost/preprocessor/comparison/greater.hpp>
27#include <boost/preprocessor/control/if.hpp>
28#include <boost/preprocessor/stringize.hpp>
29#include <boost/preprocessor/variadic/size.hpp>
30
32#include "utils/Parallel.hpp"
33#include "utils/stacktrace.hpp"
34
35namespace precice {
36namespace utils {
37
38static constexpr std::string_view ASSERT_FMT =
39 "ASSERTION FAILED\n"
40 "Location: {}\n"
41 "File: {}:{}\n"
42 "Expression: {}\n"
43 "Rank: {}\n"
44 "Arguments: {}\n"
45 "Stacktrace:\n{}\n";
46}
47} // namespace precice
48
49// Create a wrapper around assert that also aborts if NDEBUG is defined.
50#ifndef NDEBUG
51#include <cassert>
52#define PRECICE_ASSERT_WRAPPER() assert(false)
53#else
54#define PRECICE_ASSERT_WRAPPER() std::abort()
55#endif
56
61#define PRECICE_ASSERT_IMPL(check, args) \
62 do { \
63 if (!(check)) { \
64 std::cerr << precice::utils::format_or_error( \
65 precice::utils::ASSERT_FMT, \
66 BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, \
67 BOOST_PP_STRINGIZE(check), \
68 precice::utils::Parallel::getProcessRank(), \
69 args, \
70 getStacktrace()) \
71 << std::flush; \
72 std::cout.flush(); \
73 PRECICE_ASSERT_WRAPPER(); \
74 } \
75 } while (false)
76
77#define PRECICE_ASSERT_IMPL_N(check, ...) \
78 PRECICE_ASSERT_IMPL(check, PRECICE_LOG_ARGUMENTS(__VA_ARGS__))
79
80#define PRECICE_ASSERT_IMPL_1(check) \
81 PRECICE_ASSERT_IMPL(check, "none")
82
87#define PRECICE_ASSERT(...) \
88 BOOST_PP_CAT(PRECICE_ASSERT_IMPL_, BOOST_PP_IF(BOOST_PP_GREATER(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 1), N, 1)) \
89 (__VA_ARGS__)
90
91#endif // PRECICE_NO_ASSERTIONS
92
95#define PRECICE_UNREACHABLE(...) \
96 { \
97 std::cerr << ::precice::utils::format_or_error(__VA_ARGS__) << std::endl; \
98 std::abort(); \
99 }
static constexpr std::string_view ASSERT_FMT
Definition assertion.hpp:38
Main namespace of the precice library.