source: mainline/uspace/lib/cpp/include/__bits/trycatch.hpp@ e49d0ac

Last change on this file since e49d0ac was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[e7970fe]1/*
[b57ba05]2 * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
[e7970fe]3 *
[b57ba05]4 * SPDX-License-Identifier: BSD-3-Clause
[e7970fe]5 */
6
[7bbf91e]7#ifndef LIBCPP_BITS_TRYCATCH
8#define LIBCPP_BITS_TRYCATCH
[e7970fe]9
10// TODO: This header should be included in every libcpp header.
11
12/**
13 * For the time that exception support is not present
14 * in HelenOS, we use mock macros in place of the keywords
15 * try, throw and catch that allow us to atleast partially
16 * mimic exception functionality (that is, without propagation
17 * and stack unwinding).
18 * The value of the macro below determines if the keyword
19 * hiding macros get defined.
20 */
21#define LIBCPP_EXCEPTIONS_SUPPORTED 0
22
23#if LIBCPP_EXCEPTIONS_SUPPORTED == 0
24
[7bbf91e]25/**
26 * In case the file where our macros get expanded
27 * does not include cstdlib.
28 */
29extern "C" void abort(void) __attribute__((noreturn));
30extern "C" int printf(const char*, ...);
31
[e7970fe]32namespace std
33{
34 namespace aux
35 {
36 /**
37 * Monitors the state of the program with
38 * regards to exceptions.
39 */
40 extern bool exception_thrown;
41
42 inline constexpr bool try_blocks_allowed{true};
43 }
44}
45
[239d25b]46/**
47 * The language allows us to odr-use a variable
48 * that is not defined. We use this to support
49 * macros redefining the catch keyword that are
50 * followed by a block that uses that symbol.
51 *
52 * Normally, that would produce a compiler error
53 * as the declaration of that variale would be removed
54 * with the catch statement, but if we use the following
55 * declaration's name as the name of the caught exception,
56 * all will work well because of this extern declaration.
57 *
58 * Note: We do not follow our usual convention of using
59 * the aux:: namespace because that cannot be used in
60 * variable declaration.
61 */
62extern int __exception;
63
[e7970fe]64/**
65 * These macros allow us to choose how the program
66 * should behave when an exception is thrown
67 * (LIBCPP_EXCEPTION_HANDLE_THROW) or caught
68 * (LIBCPP_EXCEPTION_HANDLE_CATCH).
69 * We also provide three handlers that either
70 * hang the program (allowing us to read the
71 * message), exit the program (allowing us to
72 * redirect the message to some output file and
73 * end) or ignore the throw (in which case the
74 * state of program will be broken, but since
75 * we output messages on both and catch, this option
76 * might allow us to see which catch statement
77 * catches the "thrown exception" (supposing
78 * the program doesn't crash before reaching
79 * that statement.
80 */
81#define LIBCPP_EXCEPTION_HANG while (true);
82#define LIBCPP_EXCEPTION_ABORT ::std::abort();
83#define LIBCPP_EXCEPTION_IGNORE /* IGNORE */
84#define LIBCPP_EXCEPTION_HANDLE_THROW LIBCPP_EXCEPTION_IGNORE
[239d25b]85#define LIBCPP_EXCEPTION_HANDLE_CATCH LIBCPP_EXCEPTION_ABORT
[e7970fe]86
87#define try if constexpr (::std::aux::try_blocks_allowed)
88
89#define throw \
90 do {\
91 ::std::aux::exception_thrown = true; \
92 printf("[EXCEPTION] Thrown at %s:%d\n", __FILE__, __LINE__); \
93 LIBCPP_EXCEPTION_HANDLE_THROW \
[5df0491]94 } while (false);
[e7970fe]95
96#define catch(expr) \
97 if (::std::aux::exception_thrown) \
98 { \
99 printf("[EXCEPTION] Caught < "#expr" > at %s:%d\n", __FILE__, __LINE__); \
[4529c4b]100 ::std::aux::exception_thrown = false; \
[e7970fe]101 LIBCPP_EXCEPTION_HANDLE_CATCH \
102 } \
[4529c4b]103 if constexpr (false)
[e7970fe]104
[6fa83f0]105/**
106 * This macro can be used for testing the library. If
107 * exception handling is not available, it uses the
108 * internal bool variable and if it is, it uses a
109 * universal catch clause in which it sets the passed
110 * checking variable to true.
111 */
112#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
113 variable = ::std::aux::exception_thrown
114
115#else
116#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
117 catch (...) \
118 { \
119 variable = true; \
120 }
[e7970fe]121#endif
122
123#endif
Note: See TracBrowser for help on using the repository browser.