binutils-gdb/gdb/unittests/cli-utils-selftests.c
Tom Tromey 3d6e9d2336 Make exceptions use std::string and be self-managing
This changes the exception's "message" member to be a shared_ptr
wrapping a std::string.  This allows removing the stack of exception
messages, because now exceptions will self-destruct when needed.  This
also adds a noexcept copy constructor and operator= to gdb_exception,
plus a "what" method.

gdb/ChangeLog
2019-04-08  Tom Tromey  <tom@tromey.com>

	* xml-support.c (gdb_xml_parser::parse): Update.
	* x86-linux-nat.c (x86_linux_nat_target::enable_btrace): Update.
	* value.c (show_convenience): Update.
	* unittests/cli-utils-selftests.c (test_number_or_range_parser)
	(test_parse_flags_qcs): Update.
	* thread.c (thr_try_catch_cmd): Update.
	* target.c (target_translate_tls_address): Update.
	* stack.c (print_frame_arg, read_frame_local, read_frame_arg)
	(info_frame_command_core, frame_apply_command_count): Update.
	* rust-exp.y (rust_lex_exception_test): Update.
	* riscv-tdep.c (riscv_print_one_register_info): Update.
	* remote.c (remote_target::enable_btrace): Update.
	* record-btrace.c (record_btrace_enable_warn): Update.
	* python/py-utils.c (gdbpy_convert_exception): Update.
	* printcmd.c (do_one_display, print_variable_and_value): Update.
	* mi/mi-main.c (mi_print_exception): Update.
	* mi/mi-interp.c (mi_cmd_interpreter_exec): Use SCOPE_EXIT.
	* mi/mi-cmd-stack.c (list_arg_or_local): Update.
	* linux-nat.c (linux_nat_target::attach): Update.
	* linux-fork.c (class scoped_switch_fork_info): Update.
	* infrun.c (displaced_step_prepare): Update.
	* infcall.c (call_function_by_hand_dummy): Update.
	* guile/scm-exception.c (gdbscm_scm_from_gdb_exception): Update.
	* gnu-v3-abi.c (print_one_vtable): Update.
	* frame.c (get_prev_frame_always): Update.
	* f-valprint.c (info_common_command_for_block): Update.
	* exec.c (try_open_exec_file): Update.
	* exceptions.c (print_exception, exception_print)
	(exception_fprintf, exception_print_same): Update.
	* dwarf2-frame.c (dwarf2_build_frame_info): Update.
	* dwarf-index-cache.c (index_cache::store)
	(index_cache::lookup_gdb_index): Update.
	* darwin-nat.c (maybe_cache_shell): Update.
	* cp-valprint.c (cp_print_value_fields): Update.
	* compile/compile-cplus-symbols.c (gcc_cplus_convert_symbol)
	(gcc_cplus_symbol_address): Update.
	* compile/compile-c-symbols.c (gcc_convert_symbol)
	(gcc_symbol_address, generate_c_for_for_one_variable): Update.
	* common/selftest.c: Update.
	* common/common-exceptions.h (struct gdb_exception) <message>: Now
	a std::string.
	(exception_try_scope_entry, exception_try_scope_exit): Don't
	declare.
	(struct exception_try_scope): Remove.
	(TRY): Don't use exception_try_scope.
	(struct gdb_exception): Add constructor, operator=.
	<what>: New method.
	(struct gdb_exception_RETURN_MASK_ALL)
	(struct gdb_exception_RETURN_MASK_ERROR)
	(struct gdb_exception_RETURN_MASK_QUIT): Add constructor.
	(struct gdb_quit_bad_alloc): Update.
	* common/common-exceptions.c (exception_none): Change
	initializer.
	(struct catcher) <state, exception>: Initialize inline.
	<prev>: Remove member.
	(current_catcher): Remove.
	(catchers): New global.
	(exceptions_state_mc_init): Simplify.
	(catcher_pop): Remove.
	(exceptions_state_mc, exceptions_state_mc_catch): Update.
	(try_scope_depth, exception_try_scope_entry)
	(exception_try_scope_exit): Remove.
	(throw_exception_sjlj): Update.
	(exception_messages, exception_messages_size): Remove.
	(throw_it): Simplify.
	(gdb_exception_sliced_copy): Remove.
	(throw_exception_cxx): Update.
	* cli/cli-script.c (script_from_file): Update.
	* breakpoint.c (insert_bp_location, update_breakpoint_locations):
	Update.
	* ada-valprint.c (ada_val_print): Update.
	* ada-lang.c (ada_to_fixed_type_1, ada_exception_name_addr)
	(create_excep_cond_exprs): Update.

gdb/gdbserver/ChangeLog
2019-04-08  Tom Tromey  <tom@tromey.com>

	* server.c (handle_btrace_general_set, handle_qxfer_btrace)
	(handle_qxfer_btrace_conf, detach_or_kill_for_exit_cleanup)
	(captured_main, main): Update.
	* gdbreplay.c (main): Update.
2019-04-08 09:05:38 -06:00

254 lines
6.5 KiB
C

/* Unit tests for the cli-utils.c file.
Copyright (C) 2018-2019 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "cli/cli-utils.h"
#include "common/selftest.h"
namespace selftests {
namespace cli_utils {
static void
test_number_or_range_parser ()
{
/* Test parsing a simple integer. */
{
number_or_range_parser one ("1");
SELF_CHECK (!one.finished ());
SELF_CHECK (one.get_number () == 1);
SELF_CHECK (one.finished ());
SELF_CHECK (strcmp (one.cur_tok (), "") == 0);
}
/* Test parsing an integer followed by a non integer. */
{
number_or_range_parser one_after ("1 after");
SELF_CHECK (!one_after.finished ());
SELF_CHECK (one_after.get_number () == 1);
SELF_CHECK (one_after.finished ());
SELF_CHECK (strcmp (one_after.cur_tok (), "after") == 0);
}
/* Test parsing a range. */
{
number_or_range_parser one_three ("1-3");
for (int i = 1; i < 4; i++)
{
SELF_CHECK (!one_three.finished ());
SELF_CHECK (one_three.get_number () == i);
}
SELF_CHECK (one_three.finished ());
SELF_CHECK (strcmp (one_three.cur_tok (), "") == 0);
}
/* Test parsing a range followed by a non-integer. */
{
number_or_range_parser one_three_after ("1-3 after");
for (int i = 1; i < 4; i++)
{
SELF_CHECK (!one_three_after.finished ());
SELF_CHECK (one_three_after.get_number () == i);
}
SELF_CHECK (one_three_after.finished ());
SELF_CHECK (strcmp (one_three_after.cur_tok (), "after") == 0);
}
/* Test a negative integer gives an error. */
{
number_or_range_parser minus_one ("-1");
SELF_CHECK (!minus_one.finished ());
TRY
{
minus_one.get_number ();
SELF_CHECK (false);
}
CATCH (ex, RETURN_MASK_ERROR)
{
SELF_CHECK (ex.reason == RETURN_ERROR);
SELF_CHECK (ex.error == GENERIC_ERROR);
SELF_CHECK (strcmp (ex.what (), "negative value") == 0);
SELF_CHECK (strcmp (minus_one.cur_tok (), "-1") == 0);
}
END_CATCH;
}
/* Test that a - followed by not a number does not give an error. */
{
number_or_range_parser nan ("-whatever");
SELF_CHECK (nan.finished ());
SELF_CHECK (strcmp (nan.cur_tok (), "-whatever") == 0);
}
}
static void
test_parse_flags ()
{
const char *flags = "abc";
const char *non_flags_args = "non flags args";
/* Extract twice the same flag, separated by one space. */
{
const char *t1 = "-a -a non flags args";
SELF_CHECK (parse_flags (&t1, flags) == 1);
SELF_CHECK (parse_flags (&t1, flags) == 1);
SELF_CHECK (strcmp (t1, non_flags_args) == 0);
}
/* Extract some flags, separated by one or more spaces. */
{
const char *t2 = "-c -b -c -b -c non flags args";
SELF_CHECK (parse_flags (&t2, flags) == 3);
SELF_CHECK (parse_flags (&t2, flags) == 2);
SELF_CHECK (parse_flags (&t2, flags) == 3);
SELF_CHECK (parse_flags (&t2, flags) == 2);
SELF_CHECK (parse_flags (&t2, flags) == 3);
SELF_CHECK (strcmp (t2, non_flags_args) == 0);
}
/* Check behaviour where there is no flag to extract. */
{
const char *t3 = non_flags_args;
SELF_CHECK (parse_flags (&t3, flags) == 0);
SELF_CHECK (strcmp (t3, non_flags_args) == 0);
}
/* Extract 2 known flags in front of unknown flags. */
{
const char *t4 = "-c -b -x -y -z -c";
SELF_CHECK (parse_flags (&t4, flags) == 3);
SELF_CHECK (parse_flags (&t4, flags) == 2);
SELF_CHECK (strcmp (t4, "-x -y -z -c") == 0);
SELF_CHECK (parse_flags (&t4, flags) == 0);
SELF_CHECK (strcmp (t4, "-x -y -z -c") == 0);
}
/* Check combined flags are not recognised. */
{
const char *t5 = "-c -cb -c";
SELF_CHECK (parse_flags (&t5, flags) == 3);
SELF_CHECK (parse_flags (&t5, flags) == 0);
SELF_CHECK (strcmp (t5, "-cb -c") == 0);
}
}
static void
test_parse_flags_qcs ()
{
const char *non_flags_args = "non flags args";
/* Test parsing of 2 flags out of the known 3. */
{
const char *t1 = "-q -s non flags args";
qcs_flags flags;
SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t1.q",
&t1,
&flags) == 1);
SELF_CHECK (flags.quiet && !flags.cont && !flags.silent);
SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t1.s",
&t1,
&flags) == 1);
SELF_CHECK (flags.quiet && !flags.cont && flags.silent);
SELF_CHECK (strcmp (t1, non_flags_args) == 0);
}
/* Test parsing when there is no flag. */
{
const char *t2 = "non flags args";
qcs_flags flags;
SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t2",
&t2,
&flags) == 0);
SELF_CHECK (!flags.quiet && !flags.cont && !flags.silent);
SELF_CHECK (strcmp (t2, non_flags_args) == 0);
}
/* Test parsing stops at a negative integer. */
{
const char *t3 = "-123 non flags args";
const char *orig_t3 = t3;
qcs_flags flags;
SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t3",
&t3,
&flags) == 0);
SELF_CHECK (!flags.quiet && !flags.cont && !flags.silent);
SELF_CHECK (strcmp (t3, orig_t3) == 0);
}
/* Test mutual exclusion between -c and -s. */
{
const char *t4 = "-c -s non flags args";
qcs_flags flags;
TRY
{
SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t4.cs",
&t4,
&flags) == 1);
(void) parse_flags_qcs ("test_parse_flags_qcs.t4.cs",
&t4,
&flags);
SELF_CHECK (false);
}
CATCH (ex, RETURN_MASK_ERROR)
{
SELF_CHECK (ex.reason == RETURN_ERROR);
SELF_CHECK (ex.error == GENERIC_ERROR);
SELF_CHECK
(strcmp (ex.what (),
"test_parse_flags_qcs.t4.cs: "
"-c and -s are mutually exclusive") == 0);
}
END_CATCH;
}
}
static void
test_cli_utils ()
{
selftests::cli_utils::test_number_or_range_parser ();
selftests::cli_utils::test_parse_flags ();
selftests::cli_utils::test_parse_flags_qcs ();
}
}
}
void
_initialize_cli_utils_selftests ()
{
selftests::register_test ("cli_utils",
selftests::cli_utils::test_cli_utils);
}