Mercurial > octave
annotate liboctave/util/quit.cc @ 27471:fd32c1a9b1bd
revamp error handling
In "error" and similar functions that ultimately call it, simply throw
an exception that contains information about the error (message, id,
stack info) instead of printing an error message immediately and then
throwing an exception. The new approach is more flexible and sllows
for some simplification of the error message routines as they no
longer need feedback from the interpreter to know when to display or
buffer messages. It is now the responsibility of any code that
catches an execution exception to determine whether and when to
display error messages.
* quit.h, quit.cc (class frame_info): New class.
(execution_exception::m_err_type, execution_exception::m_id,
execution_exception::m_message, execution_exception::m_stack_info):
New data members.
(class execution_exception): Store error type, message, id, and stack
info. Provide methods setting and accessing data as needed and for
generating stack trace message from stack info.
(execution_exception::m_stack_trace): Delete data member.
execution_exception::set_stack_trace): Delete method.
(execution_exception::set_err_type, execution_exception::err_type,
execution_exception::stack_trace, execution_exception::set_identifier,
execution_exception::identifier, execution_exception::message,
execution_exception::set_stack_info, execution_exception::display):
New methods.
* call-stack.cc, call-stack.h (call_stack::backtrace_info):
New functions.
* oct-parse.yy (maybe_print_last_error_message): Delete function and
all uses.
* pt-eval.h, pt-eval.cc (tree_evaluator::backtrace_info,
tree_evaluator::backtrace_message): New functions.
(tree_evaluator::backtrace): Now const.
(tree_evaluator::visit_unwind_protect_command,
tree_evaluator::do_unwind_protect_cleanup_code,
tree_evaluator::visit_try_catch_command, tree_evaluator::evalin,
tree_evaluator::eval, tree_evaluator::repl, debugger::repl): Save
current exception info.
* interpreter.h, interpreter.cc (interpreter::handle_exception):
New function. Use it in place of direct calls to
error_system::save_exception, error_system::display_exception (or
execution_exception::display) and interpreter::recover_from_exception,
so that we have uniform behavior when responding to an execution
exception.
* error.h, error.cc (error_system::m_buffer_error_messages,
error_system::m_discard_error_messages, error_system::m_in_try_catch):
Delete data members and associated functions. Remove all uses.
Because the error system no longer displays messages immediately,
it does not need to track whether to discard or buffer error messages
or know whether error and warning functions are invoked inside of
try-catch blocks. Everywhere that catches execution_exceptions must
now handle saving the exception state (for lasterror) and displaying
the error message and traceback as needed.
(): Delete functions and all uses.
(error_stack_frame): Delete struct definition.
(verror, vpr_where, pr_where_internal, pr_where, maybe_enter_debugger,
make_execution_exception, vmessage_with_id, message_with_id,
error_system::maybe_enter_debugger, reset_error_handler,
error_system::reset): Delete functions and all uses.
(error_system::try_option): Delete enum and all uses.
(vusage, error_1, error_system::vwarning,
error_system::rethrow_error, error_system::interpreter_try):
Simplify.
(format_message, make_stack_map, error_system::throw_error,
error_system::save_exception, error_system::display_exception):
New functions.
(Ferror): Update for error_system changes.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 04 Oct 2019 01:15:13 -0400 |
parents | 00f796120a6d |
children | 63b417917f5e |
rev | line source |
---|---|
4153 | 1 /* |
2 | |
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25438
diff
changeset
|
3 Copyright (C) 2002-2019 John W. Eaton |
4153 | 4 |
5 This file is part of Octave. | |
6 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24444
diff
changeset
|
7 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24444
diff
changeset
|
9 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
10 (at your option) any later version. |
4153 | 11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
15 GNU General Public License for more details. |
4153 | 16 |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24444
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
4153 | 20 |
21 */ | |
22 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21202
diff
changeset
|
24 # include "config.h" |
4153 | 25 #endif |
26 | |
4154 | 27 #include <cstring> |
28 | |
27471 | 29 #include <ostream> |
30 #include <sstream> | |
4180 | 31 #include <new> |
32 | |
4153 | 33 #include "quit.h" |
34 | |
23795
980f39c3ab90
Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents:
23426
diff
changeset
|
35 void (*octave_signal_hook) (void) = nullptr; |
980f39c3ab90
Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents:
23426
diff
changeset
|
36 void (*octave_interrupt_hook) (void) = nullptr; |
980f39c3ab90
Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents:
23426
diff
changeset
|
37 void (*octave_bad_alloc_hook) (void) = nullptr; |
4429 | 38 |
27471 | 39 namespace octave |
40 { | |
41 std::string execution_exception::stack_trace (void) const | |
42 { | |
43 size_t nframes = m_stack_info.size (); | |
44 | |
45 if (nframes == 0) | |
46 return std::string (); | |
47 | |
48 std::ostringstream buf; | |
49 | |
50 buf << "error: called from\n"; | |
51 | |
52 for (const auto& frm : m_stack_info) | |
53 { | |
54 buf << " " << frm.fcn_name (); | |
55 | |
56 int line = frm.line (); | |
57 | |
58 if (line > 0) | |
59 { | |
60 buf << " at line " << line; | |
61 | |
62 int column = frm.column (); | |
63 | |
64 if (column > 0) | |
65 buf << " column " << column; | |
66 } | |
67 | |
68 buf << "\n"; | |
69 } | |
70 | |
71 return buf.str (); | |
72 } | |
73 | |
74 void execution_exception::display (std::ostream& os) const | |
75 { | |
76 if (! m_message.empty ()) | |
77 { | |
78 os << m_err_type << ": " << m_message; | |
79 | |
80 if (m_message.back () != '\n') | |
81 { | |
82 os << "\n"; | |
83 | |
84 std::string st = stack_trace (); | |
85 | |
86 if (! st.empty ()) | |
87 os << st; | |
88 } | |
89 } | |
90 } | |
91 } | |
92 | |
4153 | 93 void |
5142 | 94 octave_handle_signal (void) |
95 { | |
96 if (octave_signal_hook) | |
97 octave_signal_hook (); | |
98 | |
99 if (octave_interrupt_state > 0) | |
100 { | |
101 octave_interrupt_state = -1; | |
102 octave_throw_interrupt_exception (); | |
103 } | |
104 } | |
105 | |
23245
aaf5ac82979d
* quit.cc (clean_up_and_exit): Don't declare definition extern.
John W. Eaton <jwe@octave.org>
parents:
23238
diff
changeset
|
106 void |
23087
9f406f0b36da
rework clean_up_and_exit (bug #50068)
John W. Eaton <jwe@octave.org>
parents:
23084
diff
changeset
|
107 clean_up_and_exit (int exit_status, bool /* safe_to_return */) |
22094
9203833cab7d
move new interpreter class to separate file
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
108 { |
23087
9f406f0b36da
rework clean_up_and_exit (bug #50068)
John W. Eaton <jwe@octave.org>
parents:
23084
diff
changeset
|
109 exit (exit_status); |
22094
9203833cab7d
move new interpreter class to separate file
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
110 } |
9203833cab7d
move new interpreter class to separate file
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
111 |
5142 | 112 void |
4153 | 113 octave_throw_interrupt_exception (void) |
114 { | |
4429 | 115 if (octave_interrupt_hook) |
116 octave_interrupt_hook (); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
117 |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
118 throw octave::interrupt_exception (); |
4153 | 119 } |
120 | |
4180 | 121 void |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
122 octave_throw_execution_exception (void) |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
123 { |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
124 // FIXME: would a hook function be useful here? |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
125 |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
126 octave_exception_state = octave_exec_exception; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
127 |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
128 throw octave::execution_exception (); |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
129 } |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
130 |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
131 void |
4180 | 132 octave_throw_bad_alloc (void) |
133 { | |
4429 | 134 if (octave_bad_alloc_hook) |
135 octave_bad_alloc_hook (); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
136 |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
137 octave_exception_state = octave_alloc_exception; |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
138 |
4181 | 139 throw std::bad_alloc (); |
4180 | 140 } |
141 | |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
142 void |
23238
3de5efeffb86
Eliminate compilation warnings related to clean_up_and_exit fcn (bug #50265).
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
143 octave_throw_exit_exception (int exit_status, int safe_to_return) |
3de5efeffb86
Eliminate compilation warnings related to clean_up_and_exit fcn (bug #50265).
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
144 { |
3de5efeffb86
Eliminate compilation warnings related to clean_up_and_exit fcn (bug #50265).
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
145 octave_exception_state = octave_quit_exception; |
3de5efeffb86
Eliminate compilation warnings related to clean_up_and_exit fcn (bug #50265).
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
146 |
23398
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
147 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC) |
24444
53ca76c5cc8d
maint: Indent pragmas as other preprocessor directives.
John W. Eaton <jwe@octave.org>
parents:
23795
diff
changeset
|
148 # pragma GCC diagnostic push |
53ca76c5cc8d
maint: Indent pragmas as other preprocessor directives.
John W. Eaton <jwe@octave.org>
parents:
23795
diff
changeset
|
149 # pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
23398
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
150 #endif |
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
151 |
25147
5960fd1b56f1
remove C++ functions, classes, and typdefs deprecated in version 4.2
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
152 throw octave::exit_exception (exit_status, safe_to_return); |
23398
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
153 |
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
154 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC) |
24444
53ca76c5cc8d
maint: Indent pragmas as other preprocessor directives.
John W. Eaton <jwe@octave.org>
parents:
23795
diff
changeset
|
155 # pragma GCC diagnostic pop |
23398
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
156 #endif |
23238
3de5efeffb86
Eliminate compilation warnings related to clean_up_and_exit fcn (bug #50265).
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
157 } |
3de5efeffb86
Eliminate compilation warnings related to clean_up_and_exit fcn (bug #50265).
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
158 |
3de5efeffb86
Eliminate compilation warnings related to clean_up_and_exit fcn (bug #50265).
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
159 void |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
160 octave_rethrow_exception (void) |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
161 { |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
162 if (octave_interrupt_state) |
10069
c670c8be7f93
fix incorrect octave_interrupt_state escaped from liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9933
diff
changeset
|
163 { |
c670c8be7f93
fix incorrect octave_interrupt_state escaped from liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9933
diff
changeset
|
164 octave_interrupt_state = -1; |
c670c8be7f93
fix incorrect octave_interrupt_state escaped from liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9933
diff
changeset
|
165 octave_throw_interrupt_exception (); |
c670c8be7f93
fix incorrect octave_interrupt_state escaped from liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9933
diff
changeset
|
166 } |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
167 else |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
168 { |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
169 switch (octave_exception_state) |
10164
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
170 { |
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
171 case octave_exec_exception: |
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
172 octave_throw_execution_exception (); |
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
173 break; |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
174 |
10164
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
175 case octave_alloc_exception: |
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
176 octave_throw_bad_alloc (); |
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
177 break; |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
178 |
23398
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
179 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC) |
24444
53ca76c5cc8d
maint: Indent pragmas as other preprocessor directives.
John W. Eaton <jwe@octave.org>
parents:
23795
diff
changeset
|
180 # pragma GCC diagnostic push |
53ca76c5cc8d
maint: Indent pragmas as other preprocessor directives.
John W. Eaton <jwe@octave.org>
parents:
23795
diff
changeset
|
181 # pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
23398
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
182 #endif |
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
183 |
22094
9203833cab7d
move new interpreter class to separate file
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
184 case octave_quit_exception: |
23238
3de5efeffb86
Eliminate compilation warnings related to clean_up_and_exit fcn (bug #50265).
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
185 octave_throw_exit_exception (octave_exit_exception_status, |
3de5efeffb86
Eliminate compilation warnings related to clean_up_and_exit fcn (bug #50265).
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
186 octave_exit_exception_safe_to_return); |
22094
9203833cab7d
move new interpreter class to separate file
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
187 break; |
9203833cab7d
move new interpreter class to separate file
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
188 |
23398
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
189 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC) |
24444
53ca76c5cc8d
maint: Indent pragmas as other preprocessor directives.
John W. Eaton <jwe@octave.org>
parents:
23795
diff
changeset
|
190 # pragma GCC diagnostic pop |
23398
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
191 #endif |
d4ef04757ead
eliminate some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
23245
diff
changeset
|
192 |
10164
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
193 default: |
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
194 break; |
085d4e025155
libcruft/misc/quit.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10069
diff
changeset
|
195 } |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
196 } |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
197 } |