tc-i386.c fix for oss-fuzz gas fuzzing
[binutils-gdb.git] / gdb / testsuite / gdb.xml / maint-xml-dump.exp
blob7ba2753f6f451f14fb48382ec5af8c8ebfd2a888
1 # Copyright 2020-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test the 'maint print xml-tdesc' command.  This file picks up every
17 # XML file matching the pattern maint-xml-dump-*.xml (in the same
18 # directory as this script) and passes each in turn to the command
19 # 'maint print xml-tdesc'.
21 # The expected output is generated by parsing the input XML file.  The
22 # rules for changing an XML file into the expected output are:
24 # 1. Blank lines, and lines starting with a comment are stripped from
25 #    the expected output.
27 # 2. The <?xml ... ?> and <!DOCTYPE ...> entities are optional,
28 #    suitable defaults will be added if these lines are missing from
29 #    the input file.
31 # 3. A trailing comment on a line will replace the expected output for
32 #    that line but with the indentation of the line preserved.  So
33 #    this (The '|' marks the start of the line):
34 #    |    <reg name="r1" bitsize="32"/>  <!-- <reg name="r1" bitsize="32" type="int" regnum="0"/> -->
35 #    Will actually look for the following output:
36 #    |    <reg name="r1" bitsize="32" type="int" regnum="0"/>
38 # 4. Indentation of lines will be preserved so your input file needs
39 #    to follow the expected indentation.
40 require allow_xml_test
42 gdb_start
44 # Read the XML file FILENAME and produce an output pattern that should
45 # match what GDB produces with the 'maint print xml-desc' command.
46 proc build_pattern { filename } {
47     set pattern {}
49     set xml_version_line {<?xml version="1.0"?>}
50     set doc_type_line {<!DOCTYPE target SYSTEM "gdb-target.dtd">}
52     set linenum 0
53     set ifd [open "$filename" r]
54     while {[gets $ifd line] >= 0} {
55         incr linenum
57         # The <?xml .... ?> tag can only appear as the first line in
58         # the file.  If it is not present then add one to the expected
59         # output now.
60         if {$linenum == 1} {
61             if {![regexp {^<\?xml} $line]} {
62                 set pattern [string_to_regexp $xml_version_line]
63                 set xml_version_line ""
64             }
65         }
67         # If we have not yet seen a DOCTYPE line, then maybe we should
68         # be adding one?  If we find <target> then add a default
69         # DOCTYPE line, otherwise, if the XML file includes a DOCTYPE
70         # line, use that.
71         if {$doc_type_line != "" } {
72             if {[regexp {^[ \t]*<target>} $line]} {
73                 set pattern [multi_line $pattern \
74                                  [string_to_regexp $doc_type_line]]
75                 set doc_type_line ""
76             } elseif {[regexp {^[ \t]*<!DOCTYPE } $line]} {
77                 set doc_type_line ""
78             }
79         }
81         if {[regexp {^[ \t]*<!--} $line]} {
82             # Comment line, ignore it.
83         } elseif {[regexp {^[ \t]+$} $line]} {
84             # Blank line, ignore it.
85         } elseif {[regexp {^([ \t]*).*<!-- (.*) -->$} $line \
86                        matches grp1 grp2]} {
87             set pattern [multi_line \
88                              $pattern \
89                              [string_to_regexp "$grp1$grp2"]]
90         } else {
91             set pattern [multi_line \
92                              $pattern \
93                              [string_to_regexp $line]]
94         }
95     }
96     close $ifd
98     # Due to handling the <?xml ...?> tags we can end up with a stray
99     # '\r\n' at the start of the output pattern.  Remove it here.
100     if {[string range $pattern 0 1] == "\r\n"} {
101         set pattern [string range $pattern 2 end]
102     }
104     return $pattern
107 # Run over every test XML file and check the output.
108 foreach filename [lsort [glob $srcdir/$subdir/maint-xml-dump-*.xml]] {
109     set pattern [build_pattern $filename]
111     if {[is_remote host]} {
112         set test_path [remote_download host $filename]
113     } else {
114         set test_path $filename
115     }
117     verbose -log "Looking for:\n$pattern"
119     gdb_test "maint print xml-tdesc $test_path" \
120         "$pattern" "check [file tail $filename]"