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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5ab9df4 was 239d25b, checked in by Jaroslav Jindrak <dzejrou@…>, 6 years ago

cpp: add the ability to catch named exceptions when exceptions are turned off and the trycatch macros are used, change old default handler for catch

  • Property mode set to 100644
File size: 4.9 KB
Line 
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_BITS_TRYCATCH
30#define LIBCPP_BITS_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/**
48 * In case the file where our macros get expanded
49 * does not include cstdlib.
50 */
51extern "C" void abort(void) __attribute__((noreturn));
52extern "C" int printf(const char*, ...);
53
54namespace std
55{
56 namespace aux
57 {
58 /**
59 * Monitors the state of the program with
60 * regards to exceptions.
61 */
62 extern bool exception_thrown;
63
64 inline constexpr bool try_blocks_allowed{true};
65 }
66}
67
68/**
69 * The language allows us to odr-use a variable
70 * that is not defined. We use this to support
71 * macros redefining the catch keyword that are
72 * followed by a block that uses that symbol.
73 *
74 * Normally, that would produce a compiler error
75 * as the declaration of that variale would be removed
76 * with the catch statement, but if we use the following
77 * declaration's name as the name of the caught exception,
78 * all will work well because of this extern declaration.
79 *
80 * Note: We do not follow our usual convention of using
81 * the aux:: namespace because that cannot be used in
82 * variable declaration.
83 */
84extern int __exception;
85
86/**
87 * These macros allow us to choose how the program
88 * should behave when an exception is thrown
89 * (LIBCPP_EXCEPTION_HANDLE_THROW) or caught
90 * (LIBCPP_EXCEPTION_HANDLE_CATCH).
91 * We also provide three handlers that either
92 * hang the program (allowing us to read the
93 * message), exit the program (allowing us to
94 * redirect the message to some output file and
95 * end) or ignore the throw (in which case the
96 * state of program will be broken, but since
97 * we output messages on both and catch, this option
98 * might allow us to see which catch statement
99 * catches the "thrown exception" (supposing
100 * the program doesn't crash before reaching
101 * that statement.
102 */
103#define LIBCPP_EXCEPTION_HANG while (true);
104#define LIBCPP_EXCEPTION_ABORT ::std::abort();
105#define LIBCPP_EXCEPTION_IGNORE /* IGNORE */
106#define LIBCPP_EXCEPTION_HANDLE_THROW LIBCPP_EXCEPTION_IGNORE
107#define LIBCPP_EXCEPTION_HANDLE_CATCH LIBCPP_EXCEPTION_ABORT
108
109#define try if constexpr (::std::aux::try_blocks_allowed)
110
111#define throw \
112 do {\
113 ::std::aux::exception_thrown = true; \
114 printf("[EXCEPTION] Thrown at %s:%d\n", __FILE__, __LINE__); \
115 LIBCPP_EXCEPTION_HANDLE_THROW \
116 } while (false);
117
118#define catch(expr) \
119 if (::std::aux::exception_thrown) \
120 { \
121 printf("[EXCEPTION] Caught < "#expr" > at %s:%d\n", __FILE__, __LINE__); \
122 ::std::aux::exception_thrown = false; \
123 LIBCPP_EXCEPTION_HANDLE_CATCH \
124 } \
125 if constexpr (false)
126
127/**
128 * This macro can be used for testing the library. If
129 * exception handling is not available, it uses the
130 * internal bool variable and if it is, it uses a
131 * universal catch clause in which it sets the passed
132 * checking variable to true.
133 */
134#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
135 variable = ::std::aux::exception_thrown
136
137#else
138#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
139 catch (...) \
140 { \
141 variable = true; \
142 }
143#endif
144
145#endif
Note: See TracBrowser for help on using the repository browser.