Files
binutils-gdb/gdb/testsuite/gdb.base/cast-indirection.exp
Tom de Vries 5edbb6ed92 [gdb/exp] Redo cast handling for indirection
In commit ed8fd0a342 ("[gdb/exp] Fix cast handling for indirection"), I
introduced the behaviour that even though we have:
...
(gdb) p *a_loc ()
'a_loc' has unknown return type; cast the call to its declared return type
...
we get:
...
(gdb) p (char)*a_loc ()
$1 = 97 'a'
...

In other words, the unknown return type of a_loc is inferred from the cast,
effectually evaluating:
...
(gdb) p (char)*(char *)a_loc ()
...

This is convient for the case that errno is defined as:
...
 #define errno (*__errno_location ())
...
and the return type of __errno_location is unknown but the macro definition is
known, such that we can use:
...
(gdb) p (int)errno
...
instead of
...
(gdb) p *(int *)__errno_location ()
...

However, as Pedro has pointed out in post-commit review [1], this makes it
harder to reason about the semantics of an expression.

For instance, this:
...
(gdb) p (long long)*a_loc ()"
...
would be evaluated without debug info as:
...
(gdb) p (long long)*(long long *)a_loc ()"
...
but with debug info as:
...
(gdb) p (long long)*(char *)a_loc ()"
...

Fix this by instead simply erroring out for this case:
...
(gdb) p (char)*a_loc ()
'a_loc' has unknown return type; cast the call to its declared return type
...

Tested on x86_64-linux.

Approved-By: Pedro Alves <pedro@palves.net>

[1] https://sourceware.org/pipermail/gdb-patches/2024-May/208821.html
2024-05-06 14:23:25 +02:00

42 lines
1.3 KiB
Plaintext

# Copyright (C) 2024 Free Software Foundation, Inc.
# 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/>.
# Check that "p (char)*a_loc ()" is handled correctly.
standard_testfile
if { [prepare_for_testing "failed to prepare" $testfile $srcfile \
{nodebug}] == -1} {
return -1
}
if ![runto_main] {
return -1
}
gdb_test "p a_loc ()" \
"'a_loc' has unknown return type; cast the call to its declared return type"
gdb_test "p *a_loc ()" \
"'a_loc' has unknown return type; cast the call to its declared return type"
gdb_test "p *(char *)a_loc ()" " = 97 'a'"
gdb_test "p (char)*(char *)a_loc ()" " = 97 'a'"
# Regression test for PR31693.
gdb_test "p (char)*a_loc ()" \
"'a_loc' has unknown return type; cast the call to its declared return type"