Debian Codegraph @

gcc/testsuite/lib/multiline.exp

raw
1 # Copyright (C) 2015-2025 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 GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
16
17 # Testing of multiline output
18
19 # We have pre-existing testcases like this:
20 # |typedef struct _GMutex GMutex; // { dg-message "previously declared here" }
21 # (using "|" here to indicate the start of a line),
22 # generating output like this:
23 # |gcc/testsuite/g++.dg/diagnostic/wrong-tag-1.C:4:16: note: 'struct _GMutex' was previously declared here
24 # where the location of the dg-message determines the expected line at
25 # which the error should be reported.
26 #
27 # To handle rich error-reporting, we want to be able to verify that we
28 # get output like this:
29 # |gcc/testsuite/g++.dg/diagnostic/wrong-tag-1.C:4:16: note: 'struct _GMutex' was previously declared here
30 # | typedef struct _GMutex GMutex; // { dg-message "previously declared here" }
31 # | ^~~~~~~
32 # where the compiler's first line of output is as before, but in
33 # which it then echoes the source lines, adding annotations.
34 #
35 # We want to be able to write testcases that verify that the
36 # emitted source-and-annotations are sane.
37 #
38 # A complication here is that the source lines contain comments
39 # containing DejaGnu directives (such as the "dg-message" above).
40 #
41 # We punt this somewhat by only matching the beginnings of lines.
42 # so that we can write e.g.
43 # |/* { dg-begin-multiline-output "" }
44 # | typedef struct _GMutex GMutex;
45 # | ^~~~~~~
46 # | { dg-end-multiline-output "" } */
47 # to have the testsuite verify the expected output.
48
49 ############################################################################
50 # Global variables.
51 ############################################################################
52
53 # This is intended to only be used from within multiline.exp.
54 # The line number of the last dg-begin-multiline-output directive.
55 set _multiline_last_beginning_line -1
56
57 # A list of
58 # first-line-number, last-line-number, lines
59 # where each "lines" is a list of strings.
60 # This is cleared at the end of each test by gcc-dg.exp's wrapper for dg-test.
61 set multiline_expected_outputs []
62
63 # Was dg-enable-nn-line-numbers called?
64 set nn_line_numbers_enabled 0
65
66 ############################################################################
67 # Exported functions.
68 ############################################################################
69
70 # Mark the beginning of an expected multiline output
71 # All lines between this and the next dg-end-multiline-output are
72 # expected to be seen.
73
74 proc dg-begin-multiline-output { args } {
75 global _multiline_last_beginning_line
76 verbose "dg-begin-multiline-output: args: $args" 3
77 set line [expr [lindex $args 0] + 1]
78
79 # Complain if there hasn't been a dg-end-multiline-output
80 # since the last dg-begin-multiline-output
81 if { $_multiline_last_beginning_line != -1 } {
82 set last_directive_line [expr $_multiline_last_beginning_line - 1]
83 error "$last_directive_line: unterminated dg-begin-multiline-output"
84 }
85
86 set _multiline_last_beginning_line $line
87 }
88
89 # Mark the end of an expected multiline output
90 # All lines up to here since the last dg-begin-multiline-output are
91 # expected to be seen.
92 #
93 # dg-end-multiline-output comment [{ target/xfail selector }]
94
95 proc dg-end-multiline-output { args } {
96 global _multiline_last_beginning_line
97 verbose "dg-end-multiline-output: args: $args" 3
98 set first_line $_multiline_last_beginning_line
99
100 # Complain if there hasn't been a dg-begin-multiline-output
101 if { $first_line == -1 } {
102 error "[lindex $args 0]: dg-end-multiline-output without dg-begin-multiline-output"
103 return
104 }
105 set _multiline_last_beginning_line -1
106
107 set last_line [expr [lindex $args 0] - 1]
108 verbose "multiline output lines: $first_line-$last_line" 3
109
110 if { [llength $args] > 3 } {
111 error "[lindex $args 0]: too many arguments"
112 return
113 }
114
115 set maybe_x ""
116 if { [llength $args] >= 3 } {
117 switch [dg-process-target [lindex $args 2]] {
118 "F" { set maybe_x "x" }
119 "P" { set maybe_x "" }
120 "N" {
121 # If we get "N", this output doesn't apply to us so ignore it.
122 return
123 }
124 }
125 }
126
127 upvar 1 prog prog
128 verbose "prog: $prog" 3
129 # "prog" now contains the filename
130 # Load it and split it into lines
131
132 set lines [_get_lines $prog $first_line $last_line]
133
134 verbose "lines: $lines" 3
135 # Create an entry of the form: first-line, last-line, lines, maybe_x
136 set entry [list $first_line $last_line $lines $maybe_x]
137 global multiline_expected_outputs
138 lappend multiline_expected_outputs $entry
139 verbose "within dg-end-multiline-output: multiline_expected_outputs: $multiline_expected_outputs" 3
140 }
141
142 # Hook to be called by gcc-dg.exp's gcc-dg-prune to
143 # look for the expected multiline outputs, pruning them,
144 # reporting PASS for those that are found, and FAIL for
145 # those that weren't found.
146 #
147 # It returns a pruned version of its output.
148
149 proc handle-multiline-outputs { text } {
150 global multiline_expected_outputs
151 global testname_with_flags
152
153 # If dg-enable-nn-line-numbers was provided, then obscure source-margin
154 # line numbers by converting them to "NN" form.
155 set text [maybe-handle-nn-line-numbers $text]
156
157 # Remove Windows .exe suffix
158 regsub -all "(as|cc1|cc1plus|collect2|f951|ld|lto-wrapper)\.exe?:" $text {\1:} text
159
160 set index 0
161 foreach entry $multiline_expected_outputs {
162 verbose " entry: $entry" 3
163 set start_line [lindex $entry 0]
164 set end_line [lindex $entry 1]
165 set multiline [lindex $entry 2]
166 set maybe_x [lindex $entry 3]
167 verbose " multiline: $multiline" 3
168 set rexp [_build_multiline_regex $multiline $index]
169 verbose "rexp: ${rexp}" 4
170 # Escape newlines in $rexp so that we can print them in
171 # pass/fail results.
172 set escaped_regex [string map {"\n" "\\n"} $rexp]
173 verbose "escaped_regex: ${escaped_regex}" 4
174
175 set title "$testname_with_flags expected multiline pattern lines $start_line-$end_line"
176
177 # Use "regsub" to attempt to prune the pattern from $text
178 if {[regsub -line $rexp $text "" text]} {
179 # The multiline pattern was pruned.
180 ${maybe_x}pass "$title"
181 } else {
182 ${maybe_x}fail "$title"
183 }
184
185 set index [expr $index + 1]
186 }
187
188 return $text
189 }
190
191 # DejaGnu directive to enable post-processing the line numbers printed in
192 # the left-hand margin when printing the source code, converting them to
193 # "NN", e.g from:
194 #
195 # 100 | if (flag)
196 # | ^
197 # | |
198 # | (1) following 'true' branch...
199 # 101 | {
200 # 102 | foo ();
201 # | ^
202 # | |
203 # | (2) ...to here
204 #
205 # to:
206 #
207 # NN | if (flag)
208 # | ^
209 # | |
210 # | (1) following 'true' branch...
211 # NN | {
212 # NN | foo ();
213 # | ^
214 # | |
215 # | (2) ...to here
216 #
217 # This is useful e.g. when testing how interprocedural paths are printed
218 # via dg-begin/end-multiline-output, to avoid depending on precise line
219 # numbers.
220
221 proc dg-enable-nn-line-numbers { args } {
222 verbose "dg-nn-line-numbers: args: $args" 2
223 global nn_line_numbers_enabled
224 set nn_line_numbers_enabled 1
225 }
226
227 # Hook to be called by prune.exp's prune_gcc_output to convert such line
228 # numbers to "NN" form.
229 #
230 # Match substrings of the form:
231 # " 25 |"
232 # and convert them to:
233 # " NN |"
234 #
235 # It returns a copy of its input, with the above changes.
236
237 proc maybe-handle-nn-line-numbers { text } {
238 global testname_with_flags
239
240 verbose "maybe-handle-nn-line-numbers" 3
241
242 global nn_line_numbers_enabled
243 if { [expr {!$nn_line_numbers_enabled}] } {
244 verbose "nn_line_numbers_enabled false; bailing out" 3
245 return $text
246 }
247
248 verbose "maybe-handle-nn-line-numbers: text before: ${text}" 4
249
250 # dg.exp's dg-test trims leading whitespace from the output
251 # in this line:
252 # set comp_output [string trimleft $comp_output]
253 # so we can't rely on the exact leading whitespace for the
254 # first line in the output.
255 # Match initial input lines that start like:
256 # "25 |"
257 # and convert them to:
258 # " NN |"
259 set rexp2 {(^[0-9]+ \|)}
260 set count_a [regsub -all $rexp2 $text " NN |" text]
261 verbose "maybe-handle-nn-line-numbers: count_a: $count_a" 4
262
263 # Match lines that start like:
264 # " 25 |"
265 # and convert them to:
266 # " NN |"
267 set rexp {([ ]+[0-9]+ \|)}
268 set count_b [regsub -all $rexp $text " NN |" text]
269 verbose "maybe-handle-nn-line-numbers: count_b: $count_b" 4
270
271 verbose "maybe-handle-nn-line-numbers: text after: ${text}" 4
272
273 return $text
274 }
275
276 ############################################################################
277 # Internal functions
278 ############################################################################
279
280 # Load FILENAME and extract the lines from FIRST_LINE
281 # to LAST_LINE (inclusive) as a list of strings.
282
283 proc _get_lines { filename first_line last_line } {
284 verbose "_get_lines" 3
285 verbose " filename: $filename" 3
286 verbose " first_line: $first_line" 3
287 verbose " last_line: $last_line" 3
288
289 set fp [open $filename r]
290 set file_data [read $fp]
291 close $fp
292 set data [split $file_data "\n"]
293 set linenum 1
294 set lines []
295 foreach line $data {
296 verbose "line $linenum: $line" 4
297 if { $linenum >= $first_line && $linenum <= $last_line } {
298 lappend lines $line
299 }
300 set linenum [expr $linenum + 1]
301 }
302
303 return $lines
304 }
305
306 # Convert $multiline from a list of strings to a multiline regex
307 # We need to support matching arbitrary followup text on each line,
308 # to deal with comments containing DejaGnu directives.
309
310 proc _build_multiline_regex { multiline index } {
311 verbose "_build_multiline_regex: $multiline $index" 4
312
313 set rexp ""
314 foreach line $multiline {
315 verbose " line: $line" 4
316
317 # We need to escape "^" and other regexp metacharacters.
318 set line [string map {"\{re:" "("
319 ":re?\}" ")?"
320 ":re\}" ")"
321 "^" "\\^"
322 "(" "\\("
323 ")" "\\)"
324 "[" "\\["
325 "]" "\\]"
326 "{" "\\{"
327 "}" "\\}"
328 "." "\\."
329 "\\" "\\\\"
330 "?" "\\?"
331 "+" "\\+"
332 "*" "\\*"
333 "|" "\\|"} $line]
334
335 append rexp $line
336 if {[string match "*^" $line] || [string match "*~" $line]} {
337 # Assume a line containing a caret/range. This must be
338 # an exact match.
339 } else {
340 # Assume that we have a quoted source line.
341 if {![string equal "" $line] } {
342 # Support arbitrary followup text on each non-empty line,
343 # to deal with comments containing containing DejaGnu
344 # directives.
345 append rexp "\[^\\n\\r\]*"
346 }
347 }
348 append rexp "\n"
349 }
350
351 # dg.exp's dg-test trims leading whitespace from the output
352 # in this line:
353 # set comp_output [string trimleft $comp_output]
354 # so we can't rely on the exact leading whitespace for the
355 # first line in the *first* multiline regex.
356 #
357 # Trim leading whitespace from the regexp, replacing it with
358 # a "\s*", to match zero or more whitespace characters.
359 if { $index == 0 } {
360 set rexp [string trimleft $rexp]
361 set rexp "\\s*$rexp"
362 }
363
364 verbose "rexp: $rexp" 4
365
366 return $rexp
367 }