source: mainline/uspace/lib/cpp/include/internal/trycatch.hpp@ c075647a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c075647a was 4529c4b, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: actual catch bodies are not executed now because of some problems they caused (possibly temporarily)

  • Property mode set to 100644
File size: 4.3 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_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
47namespace 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 inline constexpr bool try_blocks_allowed{true};
64 }
65}
66
67/**
68 * These macros allow us to choose how the program
69 * should behave when an exception is thrown
70 * (LIBCPP_EXCEPTION_HANDLE_THROW) or caught
71 * (LIBCPP_EXCEPTION_HANDLE_CATCH).
72 * We also provide three handlers that either
73 * hang the program (allowing us to read the
74 * message), exit the program (allowing us to
75 * redirect the message to some output file and
76 * end) or ignore the throw (in which case the
77 * state of program will be broken, but since
78 * we output messages on both and catch, this option
79 * might allow us to see which catch statement
80 * catches the "thrown exception" (supposing
81 * the program doesn't crash before reaching
82 * that statement.
83 */
84#define LIBCPP_EXCEPTION_HANG while (true);
85#define LIBCPP_EXCEPTION_ABORT ::std::abort();
86#define LIBCPP_EXCEPTION_IGNORE /* IGNORE */
87#define LIBCPP_EXCEPTION_HANDLE_THROW LIBCPP_EXCEPTION_IGNORE
88#define LIBCPP_EXCEPTION_HANDLE_CATCH LIBCPP_EXCEPTION_HANG
89
90#define try if constexpr (::std::aux::try_blocks_allowed)
91
92#define throw \
93 do {\
94 ::std::aux::exception_thrown = true; \
95 printf("[EXCEPTION] Thrown at %s:%d\n", __FILE__, __LINE__); \
96 LIBCPP_EXCEPTION_HANDLE_THROW \
97 } while (false);
98
99#define catch(expr) \
100 if (::std::aux::exception_thrown) \
101 { \
102 printf("[EXCEPTION] Caught < "#expr" > at %s:%d\n", __FILE__, __LINE__); \
103 ::std::aux::exception_thrown = false; \
104 LIBCPP_EXCEPTION_HANDLE_CATCH \
105 } \
106 if constexpr (false)
107
108/**
109 * This macro can be used for testing the library. If
110 * exception handling is not available, it uses the
111 * internal bool variable and if it is, it uses a
112 * universal catch clause in which it sets the passed
113 * checking variable to true.
114 */
115#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
116 variable = ::std::aux::exception_thrown
117
118#else
119#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
120 catch (...) \
121 { \
122 variable = true; \
123 }
124#endif
125
126#endif
Note: See TracBrowser for help on using the repository browser.