preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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::utils {
36
37static constexpr std::string_view ASSERT_FMT =
38 "ASSERTION FAILED\n"
39 "Location: {}\n"
40 "File: {}:{}\n"
41 "Expression: {}\n"
42 "Rank: {}\n"
43 "Arguments: {}\n"
44 "Stacktrace:\n{}\n";
45}
46
47// Create a wrapper around assert that also aborts if NDEBUG is defined.
48#ifndef NDEBUG
49#include <cassert>
50#define PRECICE_ASSERT_WRAPPER() assert(false)
51#else
52#define PRECICE_ASSERT_WRAPPER() std::abort()
53#endif
54
59#define PRECICE_ASSERT_IMPL(check, args) \
60 do { \
61 if (!(check)) { \
62 std::cerr << precice::utils::format_or_error( \
63 precice::utils::ASSERT_FMT, \
64 BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, \
65 BOOST_PP_STRINGIZE(check), \
66 precice::utils::Parallel::getProcessRank(), \
67 args, \
68 getStacktrace()) \
69 << std::flush; \
70 std::cout.flush(); \
71 PRECICE_ASSERT_WRAPPER(); \
72 } \
73 } while (false)
74
75#define PRECICE_ASSERT_IMPL_N(check, ...) \
76 PRECICE_ASSERT_IMPL(check, PRECICE_LOG_ARGUMENTS(__VA_ARGS__))
77
78#define PRECICE_ASSERT_IMPL_1(check) \
79 PRECICE_ASSERT_IMPL(check, "none")
80
85#define PRECICE_ASSERT(...) \
86 BOOST_PP_CAT(PRECICE_ASSERT_IMPL_, BOOST_PP_IF(BOOST_PP_GREATER(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 1), N, 1)) \
87 (__VA_ARGS__)
88
89#endif // PRECICE_NO_ASSERTIONS
90
93#define PRECICE_UNREACHABLE(...) \
94 { \
95 std::cerr << ::precice::utils::format_or_error(__VA_ARGS__) << std::endl; \
96 std::abort(); \
97 }
contains precice-related utilities.
static constexpr std::string_view ASSERT_FMT
Definition assertion.hpp:37