binutils-gdb/gdb/testsuite/gdb.dwarf2/dw2-unusual-field-names.c

33 lines
875 B
C
Raw Normal View History

gdb: Allow struct fields named double The 64-bit RISC-V target currently models the floating point registers as having the following type: union riscv_double { builtin_type_ieee_single float; builtin_type_ieee_double double; } Notice the choice of names for the fields of this struct, possibly not ideal choices, as these are not valid field names in C. However, this type is only ever defined within GDB (or in the target description), and no restriction seems to exist on the field names in that case. The problem though is that currently: (gdb) info registers $ft0 ft0 {float = 0, double = 0} (raw 0x0000000000000000) (gdb) p $ft0.float $1 = 0 (gdb) p $ft0.double A syntax error in expression, near `double'. We can access the 'float' field, but not the 'double' field. This is because the string 'double' is handled differently to the string 'float' in c-exp.y. In both cases the string '$ft0' is parsed as a VARIABLE expression. In the 'float' case, the string 'float' becomes a generic NAME token in 'lex_one_token', which then allows the rule "exp '.' name" to match and the field name lookup to occur. The 'double' case is different. In order to allow parsing of the type string 'long double', the 'double' string becomes the token DOUBLE_KEYWORD. At this point there's no rule to match "exp '.' DOUBLE_KEYWORD", so we can never lookup the field named 'double'. We could rename the fields for RISC-V, and maybe that would be the best solution. However, its not hard to allow for fields named 'double', which is what this patch does. A new case is added to the 'field_name' rule to match the DOUBLE_KEYWORD, and create a suitable 'struct stoken'. With this done the "exp '.' field_name" pattern can now match, and we can lookup the double field. With this patch in place I now see this behaviour: (gdb) info registers $ft0 ft0 {float = 0, double = 0} (raw 0x0000000000000000) (gdb) p $ft0.float $1 = 0 (gdb) p $ft0.double $2 = 0 I've gone ahead and handled INT_KEYWORD, LONG, SHORT, SIGNED_KEYWORD, and UNSIGNED as well within field_name. I've added a new test for this functionality. This change was tested on x86-64 GNU/Linux with no regressions. gdb/ChangeLog: * c-exp.y (field_name): Allow DOUBLE_KEYWORD, INT_KEYWORD, LONG, SHORT, SIGNED_KEYWORD, and UNSIGNED tokens to act as a field names. (typename_stoken): New function. gdb/testsuite/ChangeLog: * gdb.dwarf2/dw2-unusual-field-names.c: New file. * gdb.dwarf2/dw2-unusual-field-names.exp: New file.
2018-12-13 19:25:25 +01:00
/* This testcase is part of GDB, the GNU debugger.
Copyright 2018-2020 Free Software Foundation, Inc.
gdb: Allow struct fields named double The 64-bit RISC-V target currently models the floating point registers as having the following type: union riscv_double { builtin_type_ieee_single float; builtin_type_ieee_double double; } Notice the choice of names for the fields of this struct, possibly not ideal choices, as these are not valid field names in C. However, this type is only ever defined within GDB (or in the target description), and no restriction seems to exist on the field names in that case. The problem though is that currently: (gdb) info registers $ft0 ft0 {float = 0, double = 0} (raw 0x0000000000000000) (gdb) p $ft0.float $1 = 0 (gdb) p $ft0.double A syntax error in expression, near `double'. We can access the 'float' field, but not the 'double' field. This is because the string 'double' is handled differently to the string 'float' in c-exp.y. In both cases the string '$ft0' is parsed as a VARIABLE expression. In the 'float' case, the string 'float' becomes a generic NAME token in 'lex_one_token', which then allows the rule "exp '.' name" to match and the field name lookup to occur. The 'double' case is different. In order to allow parsing of the type string 'long double', the 'double' string becomes the token DOUBLE_KEYWORD. At this point there's no rule to match "exp '.' DOUBLE_KEYWORD", so we can never lookup the field named 'double'. We could rename the fields for RISC-V, and maybe that would be the best solution. However, its not hard to allow for fields named 'double', which is what this patch does. A new case is added to the 'field_name' rule to match the DOUBLE_KEYWORD, and create a suitable 'struct stoken'. With this done the "exp '.' field_name" pattern can now match, and we can lookup the double field. With this patch in place I now see this behaviour: (gdb) info registers $ft0 ft0 {float = 0, double = 0} (raw 0x0000000000000000) (gdb) p $ft0.float $1 = 0 (gdb) p $ft0.double $2 = 0 I've gone ahead and handled INT_KEYWORD, LONG, SHORT, SIGNED_KEYWORD, and UNSIGNED as well within field_name. I've added a new test for this functionality. This change was tested on x86-64 GNU/Linux with no regressions. gdb/ChangeLog: * c-exp.y (field_name): Allow DOUBLE_KEYWORD, INT_KEYWORD, LONG, SHORT, SIGNED_KEYWORD, and UNSIGNED tokens to act as a field names. (typename_stoken): New function. gdb/testsuite/ChangeLog: * gdb.dwarf2/dw2-unusual-field-names.c: New file. * gdb.dwarf2/dw2-unusual-field-names.exp: New file.
2018-12-13 19:25:25 +01:00
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/>. */
struct foo
{
int field;
};
struct foo obj = { 0 };
struct foo *ptr = &obj;
int
main (void)
{
return obj.field;
}