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

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

cpp: added a macro that can be used for testing to check if an object was thrown

  • Property mode set to 100644
File size: 4.7 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 /**
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/**
99 * Since we cannot use the thrown object in this macro,
100 * we have to silence compiler warnings on it by
101 * using (void)thrown-object.
102 */
103#define throw \
104 do {\
105 ::std::aux::exception_thrown = true; \
106 printf("[EXCEPTION] Thrown at %s:%d\n", __FILE__, __LINE__); \
107 LIBCPP_EXCEPTION_HANDLE_THROW \
108 } while (false); (void)
109
110#define catch(expr) \
111 if (::std::aux::exception_thrown) \
112 { \
113 printf("[EXCEPTION] Caught < "#expr" > at %s:%d\n", __FILE__, __LINE__); \
114 LIBCPP_EXCEPTION_HANDLE_CATCH \
115 } \
116 if constexpr (expr = {}; ::std::aux::catch_blocks_allowed)
117
118/**
119 * This macro can be used for testing the library. If
120 * exception handling is not available, it uses the
121 * internal bool variable and if it is, it uses a
122 * universal catch clause in which it sets the passed
123 * checking variable to true.
124 */
125#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
126 variable = ::std::aux::exception_thrown
127
128#else
129#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
130 catch (...) \
131 { \
132 variable = true; \
133 }
134#endif
135
136#endif
Note: See TracBrowser for help on using the repository browser.