diff options
author | Pedro Alves <palves@redhat.com> | 2019-07-03 16:57:49 +0100 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2019-07-03 16:59:24 +0100 |
commit | 3d9be6f531db395a5ad940ef06e56d849f4de646 (patch) | |
tree | bfb290fae432f5dcc6186dba817d093ba5b872d6 /gdb/testsuite/gdb.base | |
parent | 41fc454c915057d9c5536617370c5eb2a5f71323 (diff) | |
download | binutils-gdb-3d9be6f531db395a5ad940ef06e56d849f4de646.tar.gz |
Teach gdb::option about string options
A following patch will make the "pipe" command use the gdb::option
framework for option processing. However, "pipe"'s only option today
is a string option, "-d DELIM", and gdb::option does not support
string options yet.
This commit adds support for string options, mapped to var_string.
For now, a string is parsed up until the first whitespace. I imagine
that we'll need to add support for quoting so that we could do:
(gdb) cmd -option 'some -string'
without gdb confusing the "-string" for an option.
This doesn't seem important for pipe, so I'm leaving it for another
day.
One thing I'm not happy with, is that the string data is managed as a
raw malloc-allocated char *, which means that we need to xfree it
manually. This is because var_string settings work that way too.
Although with var_string settings we're leaking the strings at gdb
exit, that was never really a problem. For options though, leaking is
undesirable.
I think we should tackle that for both settings and options at the
same time, so for now I'm just managing the malloced data manually.
It's a bit ugly in option_def_and_value, but at least that's hidden
from view.
For testing, this adds a new "-string" option to "maint
test-settings", and then tweaks gdb.base/options.exp to exercise it.
gdb/ChangeLog:
2019-07-03 Pedro Alves <palves@redhat.com>
* cli/cli-option.c (union option_value) <string>: New field.
(struct option_def_and_value): Add ctor, move ctor, dtor and
use DISABLE_COPY_AND_ASSIGN.
(option_def_and_value::clear_value): New.
(parse_option, save_option_value_in_ctx, get_val_type_str)
(add_setshow_cmds_for_options): Handle var_string.
* cli-option.h (union option_def::var_address) <string>: New
field.
(struct string_option_def): New.
* maint-test-options.c (struct test_options_opts): Add default
ctor and use DISABLE_COPY_AND_ASSIGN.
<string_opt>: New field.
(test_options_opts::~test_options_opts): New.
(test_options_opts::dump): Also dump "-string".
(test_options_option_defs): Install "string.
gdb/testsuite/ChangeLog:
2019-07-03 Pedro Alves <palves@redhat.com>
* gdb.base/options.exp (expect_none, expect_flag, expect_bool)
(expect_integer): Adjust to expect "-string".
(expect_string): New.
(all_options): Expect "-string".
(test-flag, test-boolean): Adjust to expect "-string".
(test-string): New proc.
(top level): Call it.
Diffstat (limited to 'gdb/testsuite/gdb.base')
-rw-r--r-- | gdb/testsuite/gdb.base/options.exp | 82 |
1 files changed, 65 insertions, 17 deletions
diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/options.exp index 1a652b3c9dc..e8f571d9ba9 100644 --- a/gdb/testsuite/gdb.base/options.exp +++ b/gdb/testsuite/gdb.base/options.exp @@ -95,19 +95,19 @@ proc make_cmd {variant} { # test-options xxx", with no flag/option set. OPERAND is the expected # operand. proc expect_none {operand} { - return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- $operand" + return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -string '' -- $operand" } # Return a string for the expected result of running "maint # test-options xxx", with -flag set. OPERAND is the expected operand. proc expect_flag {operand} { - return "-flag 1 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- $operand" + return "-flag 1 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -string '' -- $operand" } # Return a string for the expected result of running "maint # test-options xxx", with -bool set. OPERAND is the expected operand. proc expect_bool {operand} { - return "-flag 0 -xx1 0 -xx2 0 -bool 1 -enum xxx -uint 0 -zuint-unl 0 -- $operand" + return "-flag 0 -xx1 0 -xx2 0 -bool 1 -enum xxx -uint 0 -zuint-unl 0 -string '' -- $operand" } # Return a string for the expected result of running "maint @@ -116,18 +116,26 @@ proc expect_bool {operand} { # expected operand. proc expect_integer {option val operand} { if {$option == "uinteger"} { - return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint $val -zuint-unl 0 -- $operand" + return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint $val -zuint-unl 0 -string '' -- $operand" } elseif {$option == "zuinteger-unlimited"} { - return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl $val -- $operand" + return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl $val -string '' -- $operand" } else { error "unsupported option: $option" } } +# Return a string for the expected result of running "maint +# test-options xxx", with -string set to $STR. OPERAND is the +# expected operand. +proc expect_string {str operand} { + return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -string '$str' -- $operand" +} + set all_options { "-bool" "-enum" "-flag" + "-string" "-uinteger" "-xx1" "-xx2" @@ -577,7 +585,7 @@ proc_with_prefix test-flag {variant} { # Extract twice the same flag, separated by one space. gdb_test "$cmd -xx1 -xx2 -xx1 -xx2 -xx1 -- non flags args" \ - "-flag 0 -xx1 1 -xx2 1 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- non flags args" + "-flag 0 -xx1 1 -xx2 1 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -string '' -- non flags args" # Extract 2 known flags in front of unknown flags. gdb_test "$cmd -xx1 -xx2 -a -b -c -xx1 --" \ @@ -624,19 +632,11 @@ proc_with_prefix test-boolean {variant} { # E.g., "frame apply all -past-main COMMAND". if {$variant == "require-delimiter"} { + set match_list $all_options + lappend match_list "off" "on" res_test_gdb_complete_multiple \ "1 [expect_none ""]" \ - "$cmd -bool " "" "" { - "-bool" - "-enum" - "-flag" - "-uinteger" - "-xx1" - "-xx2" - "-zuinteger-unlimited" - "off" - "on" - } + "$cmd -bool " "" "" $match_list } else { res_test_gdb_complete_none "0 " "$cmd -bool " } @@ -942,6 +942,53 @@ proc_with_prefix test-enum {variant} { gdb_test "$cmd -enum www --" "Undefined item: \"www\"." } +# String option tests. +proc_with_prefix test-string {variant} { + global all_options + + set cmd [make_cmd $variant] + + res_test_gdb_complete_none \ + "1 [expect_none ""]" \ + "$cmd -string " + + # Check that "-" where a value is expected does not show the + # command's options. I.e., a string's value is not optional. + # Check both completion and running the command. + res_test_gdb_complete_none \ + "1 [expect_none ""]" \ + "$cmd -string -" + gdb_test "$cmd -string --"\ + "-string requires an argument" + if {$variant == "require-delimiter"} { + gdb_test "$cmd -string" [expect_none "-string"] + } else { + gdb_test "$cmd -string"\ + "-string requires an argument" + } + + res_test_gdb_complete_none \ + "1 [expect_none ""]" \ + "$cmd -string STR" + gdb_test "$cmd -string STR --" [expect_string "STR" ""] + + # Completing at "-" after parsing STR should list all options. + res_test_gdb_complete_multiple \ + "1 [expect_string "STR" "-"]" \ + "$cmd -string STR " "-" "" $all_options + + # Check that only FOO is considered part of the string's value. + # I.e., that we stop parsing the string at the first whitespace. + if {$variant == "require-delimiter"} { + res_test_gdb_complete_none \ + "1 [expect_string "FOO" "BAR"]" \ + "$cmd -string FOO BAR" + } else { + res_test_gdb_complete_none "0 BAR" "$cmd -string FOO BAR" + } + gdb_test "$cmd -string FOO BAR --" "Unrecognized option at: BAR --" +} + # Run the options framework tests first. foreach_with_prefix cmd { "require-delimiter" @@ -955,6 +1002,7 @@ foreach_with_prefix cmd { test-uinteger $cmd $subcmd } test-enum $cmd + test-string $cmd } # Run the print integration tests, both as "standalone", and under |