From e234f809714ea782ed50da734b1dc6b379ff67ad Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Fri, 17 Aug 2018 11:34:42 +0200 Subject: [PATCH] Move global deal_II_exceptions variables into the internals namespace --- include/deal.II/base/exceptions.h | 78 +++++++++++++++++-------------- source/base/exceptions.cc | 33 +++++++------ tests/tests.h | 9 ++-- 3 files changed, 66 insertions(+), 54 deletions(-) diff --git a/include/deal.II/base/exceptions.h b/include/deal.II/base/exceptions.h index a929f1d5c3..0c20aacc57 100644 --- a/include/deal.II/base/exceptions.h +++ b/include/deal.II/base/exceptions.h @@ -1020,14 +1020,17 @@ namespace StandardExceptions */ namespace deal_II_exceptions { - /** - * Setting this variable to false will disable deal.II's exception mechanism - * to abort the problem. The Assert() macro will throw the exception instead - * and the AssertNothrow() macro will just print the error message. This - * variable should not be changed directly. Use disable_abort_on_exception() - * instead. - */ - extern bool allow_abort_on_exception; + namespace internals + { + /** + * Setting this variable to false will disable deal.II's exception mechanism + * to abort the problem. The Assert() macro will throw the exception instead + * and the AssertNothrow() macro will just print the error message. This + * variable should not be changed directly. Use disable_abort_on_exception() + * instead. + */ + extern bool allow_abort_on_exception; + } // namespace internals /** * Set a string that is printed upon output of the message indicating a @@ -1093,7 +1096,7 @@ namespace deal_II_exceptions * error message provided by @p exc and calling std::abort(). */ [[noreturn]] void - internal_abort(const ExceptionBase &exc) noexcept; + abort(const ExceptionBase &exc) noexcept; /** * An enum describing how to treat an exception in issue_error_noreturn. @@ -1105,7 +1108,7 @@ namespace deal_II_exceptions * deal_II_exceptions::disable_abort_on_exception has been called: in * that case the program will throw an exception. */ - try_abort_on_exception, + abort_or_throw_on_exception, /** * Throw the exception normally. */ @@ -1115,7 +1118,7 @@ namespace deal_II_exceptions /** * This routine does the main work for the exception generation mechanism * used in the Assert and AssertThrow macros: as the - * name implies, this function either ends throwing an exception (if + * name implies, this function either ends by throwing an exception (if * @p handling is throw_on_exception, or @p handling is try_abort_exception * and deal_II_exceptions::disable_abort_on_exception is false) or with a * call to abort (if @p handling is try_abort_exception and @@ -1143,10 +1146,11 @@ namespace deal_II_exceptions switch (handling) { - case try_abort_on_exception: + case abort_or_throw_on_exception: { - if (dealii::deal_II_exceptions::allow_abort_on_exception) - internals::internal_abort(e); + if (dealii::deal_II_exceptions::internals:: + allow_abort_on_exception) + internals::abort(e); else { // We are not allowed to abort, so just throw the error: @@ -1232,30 +1236,32 @@ namespace deal_II_exceptions */ #ifdef DEBUG # ifdef DEAL_II_HAVE_BUILTIN_EXPECT -# define Assert(cond, exc) \ - { \ - if (__builtin_expect(!(cond), false)) \ - ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \ - ::dealii::deal_II_exceptions::internals::try_abort_on_exception, \ - __FILE__, \ - __LINE__, \ - __PRETTY_FUNCTION__, \ - #cond, \ - #exc, \ - exc); \ +# define Assert(cond, exc) \ + { \ + if (__builtin_expect(!(cond), false)) \ + ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \ + ::dealii::deal_II_exceptions::internals:: \ + abort_or_throw_on_exception, \ + __FILE__, \ + __LINE__, \ + __PRETTY_FUNCTION__, \ + #cond, \ + #exc, \ + exc); \ } # else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/ -# define Assert(cond, exc) \ - { \ - if (!(cond)) \ - ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \ - ::dealii::deal_II_exceptions::internals::try_abort_on_exception, \ - __FILE__, \ - __LINE__, \ - __PRETTY_FUNCTION__, \ - #cond, \ - #exc, \ - exc); \ +# define Assert(cond, exc) \ + { \ + if (!(cond)) \ + ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \ + ::dealii::deal_II_exceptions::internals:: \ + abort_or_throw_on_exception, \ + __FILE__, \ + __LINE__, \ + __PRETTY_FUNCTION__, \ + #cond, \ + #exc, \ + exc); \ } # endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/ #else diff --git a/source/base/exceptions.cc b/source/base/exceptions.cc index 753f144b29..817bd1cd68 100644 --- a/source/base/exceptions.cc +++ b/source/base/exceptions.cc @@ -41,30 +41,32 @@ DEAL_II_NAMESPACE_OPEN namespace deal_II_exceptions { - std::string additional_assert_output; + namespace internals + { + std::string additional_assert_output; + + bool show_stacktrace = true; + + bool allow_abort_on_exception = true; + } // namespace internals void set_additional_assert_output(const char *const p) { - additional_assert_output = p; + internals::additional_assert_output = p; } - bool show_stacktrace = true; - void suppress_stacktrace_in_exceptions() { - show_stacktrace = false; + internals::show_stacktrace = false; } - bool allow_abort_on_exception = true; - void disable_abort_on_exception() { - allow_abort_on_exception = false; + internals::allow_abort_on_exception = false; } - } // namespace deal_II_exceptions @@ -219,7 +221,7 @@ ExceptionBase::print_stack_trace(std::ostream &out) const if (n_stacktrace_frames == 0) return; - if (deal_II_exceptions::show_stacktrace == false) + if (deal_II_exceptions::internals::show_stacktrace == false) return; // if there is a stackframe stored, print it @@ -322,12 +324,13 @@ ExceptionBase::generate_message() const print_info(converter); print_stack_trace(converter); - if (!deal_II_exceptions::additional_assert_output.empty()) + if (!deal_II_exceptions::internals::additional_assert_output.empty()) { converter << "--------------------------------------------------------" << std::endl - << deal_II_exceptions::additional_assert_output << std::endl; + << deal_II_exceptions::internals::additional_assert_output + << std::endl; } converter << "--------------------------------------------------------" @@ -402,7 +405,7 @@ namespace deal_II_exceptions namespace internals { [[noreturn]] void - internal_abort(const ExceptionBase &exc) noexcept + abort(const ExceptionBase &exc) noexcept { // first print the error std::cerr << exc.what() << std::endl; @@ -450,8 +453,8 @@ namespace deal_II_exceptions void do_issue_error_nothrow(const ExceptionBase &exc) noexcept { - if (dealii::deal_II_exceptions::allow_abort_on_exception) - internal_abort(exc); + if (deal_II_exceptions::internals::allow_abort_on_exception) + abort(exc); else { // We are not allowed to throw, and not allowed to abort. diff --git a/tests/tests.h b/tests/tests.h index a3e2266953..25ccf10ff5 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -601,7 +601,10 @@ struct MPILogInitAll DEAL_II_NAMESPACE_OPEN namespace deal_II_exceptions { - extern bool show_stacktrace; + namespace internals + { + extern bool show_stacktrace; + } } // namespace deal_II_exceptions DEAL_II_NAMESPACE_CLOSE @@ -619,8 +622,8 @@ new_tbb_assertion_handler(const char *file, std::cerr << "Detailed description: " << comment << std::endl; // Reenable abort and stacktraces: - deal_II_exceptions::allow_abort_on_exception = true; - deal_II_exceptions::show_stacktrace = true; + deal_II_exceptions::internals::allow_abort_on_exception = true; + deal_II_exceptions::internals::show_stacktrace = true; // And abort with a deal.II exception: Assert(false, ExcMessage("TBB Exception, see above")); -- 2.39.5