Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / flexible-array-member.exp
1 # Copyright 2020-2022 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # Test getting the range of flexible array members in Python.
17
18 standard_testfile
19
20 if { [prepare_for_testing "failed to prepare" \
21 ${testfile} ${srcfile}] } {
22 return
23 }
24
25 # Skip all tests if Python scripting is not enabled.
26 if { [skip_python_tests] } { continue }
27
28 if { ![runto break_here] } {
29 untested "could not run to break_here"
30 return
31 }
32
33 # The various cases are:
34 #
35 # - ns: flexible array member with no size
36 # - zs: flexible array member with size 0 (GNU C extension that predates the
37 # standardization of the feature, but widely supported)
38 # - zso: zero-size only, a corner case where the array is the sole member of
39 # the structure
40
41 gdb_test "python ns = gdb.parse_and_eval('ns').dereference()"
42 gdb_test "python zs = gdb.parse_and_eval('zs').dereference()"
43 gdb_test "python zso = gdb.parse_and_eval('zso').dereference()"
44
45 # Print the whole structure.
46
47 gdb_test "python print(ns)" "{n = 3, items = $hex}"
48 gdb_test "python print(zs)" "{n = 3, items = $hex}"
49 gdb_test "python print(zso)" "{items = $hex}"
50
51 # Print all items.
52
53 gdb_test "python print(ns\['items'\])" "$hex"
54 gdb_test "python print(ns\['items'\]\[0\])" "101"
55 gdb_test "python print(ns\['items'\]\[1\])" "102"
56 gdb_test "python print(ns\['items'\]\[2\])" "103"
57
58 gdb_test "python print(zs\['items'\])" "$hex"
59 gdb_test "python print(zs\['items'\]\[0\])" "201"
60 gdb_test "python print(zs\['items'\]\[1\])" "202"
61 gdb_test "python print(zs\['items'\]\[2\])" "203"
62
63 gdb_test "python print(zso\['items'\])" "$hex"
64 gdb_test "python print(zso\['items'\]\[0\])" "301"
65 gdb_test "python print(zso\['items'\]\[1\])" "302"
66 gdb_test "python print(zso\['items'\]\[2\])" "303"
67
68 # Check taking the address of array elements (how PR 28675 was originally
69 # reported).
70
71 gdb_test "python print(ns\['items'\] == ns\['items'\]\[0\].address)" "True"
72 gdb_test "python print(ns\['items'\]\[0\].address + 1 == ns\['items'\]\[1\].address)" "True"
73 gdb_test "python print(zs\['items'\] == zs\['items'\]\[0\].address)" "True"
74 gdb_test "python print(zs\['items'\]\[0\].address + 1 == zs\['items'\]\[1\].address)" "True"
75 gdb_test "python print(zso\['items'\] == zso\['items'\]\[0\].address)" "True"
76 gdb_test "python print(zso\['items'\]\[0\].address + 1 == zso\['items'\]\[1\].address)" "True"
77
78 # Verify the range attribute. It looks a bit inconsistent that the high bound
79 # is sometimes 0, sometimes -1. It depends on the way the flexible array
80 # member is specified and on the compiler version (the debug info is
81 # different). But that's what GDB produces today, so that's what we test.
82
83 gdb_test "python print(ns\['items'\].type.range())" "\\(0, 0\\)"
84 gdb_test "python print(zs\['items'\].type.range())" "\\(0, (0|-1)\\)"
85 gdb_test "python print(zso\['items'\].type.range())" "\\(0, (0|-1)\\)"
86
87 # Test the same thing, but going explicitly through the array index's range
88 # type.
89
90 gdb_test "python print(ns\['items'\].type.fields()\[0\].type.range())" "\\(0, 0\\)"
91 gdb_test "python print(zs\['items'\].type.fields()\[0\].type.range())" "\\(0, (0|-1)\\)"
92 gdb_test "python print(zso\['items'\].type.fields()\[0\].type.range())" "\\(0, (0|-1)\\)"
This page took 0.032974 seconds and 4 git commands to generate.