Error.hpp
Go to the documentation of this file.
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 /// \file
5 /// Defines macro ERROR.
6 
7 #pragma once
8 
9 #include <sstream>
10 #include <string>
11 
13 #include "Parallel/Abort.hpp"
14 #include "Utilities/Literals.hpp"
15 
16 /*!
17  * \ingroup ErrorHandlingGroup
18  * \brief prints an error message to the standard error stream and aborts the
19  * program.
20  *
21  * ERROR should not be used for coding errors, but instead for user errors
22  * or failure modes of numerical algorithms. An acceptable use for error is also
23  * in the default case of a switch statement.
24  * \param m an arbitrary output stream.
25  */
26 // isocpp.org recommends using an `if (true)` instead of a `do
27 // while(false)` for macros because the latter can mess with inlining
28 // in some (old?) compilers:
29 // https://isocpp.org/wiki/faq/misc-technical-issues#macros-with-multi-stmts
30 // https://isocpp.org/wiki/faq/misc-technical-issues#macros-with-if
31 // However, Intel's reachability analyzer (as of version 16.0.3
32 // 20160415) can't figure out that the else branch and everything
33 // after it is unreachable, causing warnings (and possibly suboptimal
34 // code generation).
35 #define ERROR(m) \
36  do { \
37  std::ostringstream avoid_name_collisions_ERROR; \
38  /* clang-tidy: macro arg in parentheses */ \
39  avoid_name_collisions_ERROR << m; /* NOLINT */ \
40  abort_with_error_message(__FILE__, __LINE__, \
41  static_cast<const char*>(__PRETTY_FUNCTION__), \
42  avoid_name_collisions_ERROR.str()); \
43  } while (false)
44 
45 /*!
46  * \ingroup ErrorHandlingGroup
47  * \brief prints an error message to the standard error and aborts the
48  * program.
49  *
50  * CERROR is just like ERROR and so the same guidelines apply. However, because
51  * it does not use std::stringstream it can be used in some constexpr
52  * functions where ERROR cannot be.
53  * \param m error message as a string, may need to use string literals
54  */
55 #define CERROR(m) \
56  do { \
57  Parallel::abort("\n################ ERROR ################\nLine: "s + \
58  std::to_string(__LINE__) + " of file '"s + __FILE__ + \
59  "'\n"s + m + /* NOLINT */ \
60  "\n#######################################\n"s); \
61  } while (false)
Defines useful literals.
Defines function Parallel::abort.
Declares function abort_with_error_message.