1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_INTERNAL_TRYCATCH
|
---|
30 | #define LIBCPP_INTERNAL_TRYCATCH
|
---|
31 |
|
---|
32 | // TODO: This header should be included in every libcpp header.
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * For the time that exception support is not present
|
---|
36 | * in HelenOS, we use mock macros in place of the keywords
|
---|
37 | * try, throw and catch that allow us to atleast partially
|
---|
38 | * mimic exception functionality (that is, without propagation
|
---|
39 | * and stack unwinding).
|
---|
40 | * The value of the macro below determines if the keyword
|
---|
41 | * hiding macros get defined.
|
---|
42 | */
|
---|
43 | #define LIBCPP_EXCEPTIONS_SUPPORTED 0
|
---|
44 |
|
---|
45 | #if LIBCPP_EXCEPTIONS_SUPPORTED == 0
|
---|
46 |
|
---|
47 | namespace std
|
---|
48 | {
|
---|
49 | /**
|
---|
50 | * In case the file where our macros get expanded
|
---|
51 | * does not include cstdlib.
|
---|
52 | */
|
---|
53 | extern "C" void abort(void) __attribute__((noreturn));
|
---|
54 |
|
---|
55 | namespace aux
|
---|
56 | {
|
---|
57 | /**
|
---|
58 | * Monitors the state of the program with
|
---|
59 | * regards to exceptions.
|
---|
60 | */
|
---|
61 | extern bool exception_thrown;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * These two variables can be used to suppress
|
---|
65 | * the execution of eithe the try blocks
|
---|
66 | * or the catch blocks.
|
---|
67 | */
|
---|
68 | inline constexpr bool try_blocks_allowed{true};
|
---|
69 | inline constexpr bool catch_blocks_allowed{false};
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * These macros allow us to choose how the program
|
---|
75 | * should behave when an exception is thrown
|
---|
76 | * (LIBCPP_EXCEPTION_HANDLE_THROW) or caught
|
---|
77 | * (LIBCPP_EXCEPTION_HANDLE_CATCH).
|
---|
78 | * We also provide three handlers that either
|
---|
79 | * hang the program (allowing us to read the
|
---|
80 | * message), exit the program (allowing us to
|
---|
81 | * redirect the message to some output file and
|
---|
82 | * end) or ignore the throw (in which case the
|
---|
83 | * state of program will be broken, but since
|
---|
84 | * we output messages on both and catch, this option
|
---|
85 | * might allow us to see which catch statement
|
---|
86 | * catches the "thrown exception" (supposing
|
---|
87 | * the program doesn't crash before reaching
|
---|
88 | * that statement.
|
---|
89 | */
|
---|
90 | #define LIBCPP_EXCEPTION_HANG while (true);
|
---|
91 | #define LIBCPP_EXCEPTION_ABORT ::std::abort();
|
---|
92 | #define LIBCPP_EXCEPTION_IGNORE /* IGNORE */
|
---|
93 | #define LIBCPP_EXCEPTION_HANDLE_THROW LIBCPP_EXCEPTION_IGNORE
|
---|
94 | #define LIBCPP_EXCEPTION_HANDLE_CATCH LIBCPP_EXCEPTION_IGNORE
|
---|
95 |
|
---|
96 | #define try if constexpr (::std::aux::try_blocks_allowed)
|
---|
97 |
|
---|
98 | #define throw \
|
---|
99 | do {\
|
---|
100 | ::std::aux::exception_thrown = true; \
|
---|
101 | printf("[EXCEPTION] Thrown at %s:%d\n", __FILE__, __LINE__); \
|
---|
102 | LIBCPP_EXCEPTION_HANDLE_THROW \
|
---|
103 | } while (false);
|
---|
104 |
|
---|
105 | #define catch(expr) \
|
---|
106 | if (::std::aux::exception_thrown) \
|
---|
107 | { \
|
---|
108 | printf("[EXCEPTION] Caught < "#expr" > at %s:%d\n", __FILE__, __LINE__); \
|
---|
109 | LIBCPP_EXCEPTION_HANDLE_CATCH \
|
---|
110 | } \
|
---|
111 | if constexpr (::std::aux::catch_blocks_allowed)
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * This macro can be used for testing the library. If
|
---|
115 | * exception handling is not available, it uses the
|
---|
116 | * internal bool variable and if it is, it uses a
|
---|
117 | * universal catch clause in which it sets the passed
|
---|
118 | * checking variable to true.
|
---|
119 | */
|
---|
120 | #define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
|
---|
121 | variable = ::std::aux::exception_thrown
|
---|
122 |
|
---|
123 | #else
|
---|
124 | #define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
|
---|
125 | catch (...) \
|
---|
126 | { \
|
---|
127 | variable = true; \
|
---|
128 | }
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | #endif
|
---|