Debian Codegraph @

gcc/testsuite/lib/gdc-utils.exp

raw
1 # Copyright (C) 2012-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 # Test using the DMD testsuite.
18
19 #
20 # Convert DMD arguments to GDC equivalent
21 #
22
23 proc gdc-convert-args { args } {
24 set out ""
25
26 foreach arg [split [lindex $args 0] " "] {
27 # List of switches kept in ASCII collated order.
28 if [string match "-D" $arg] {
29 upvar 1 compilable_output_file_ext compilable_output_file_ext
30 set compilable_output_file_ext "html"
31 lappend out "-fdoc"
32
33 } elseif [string match "-H" $arg] {
34 upvar 1 compilable_output_file_ext compilable_output_file_ext
35 set compilable_output_file_ext "di"
36 lappend out "-H"
37
38 } elseif [string match "-HC" $arg] {
39 upvar 1 compilable_output_file_ext compilable_output_file_ext
40 upvar 1 name name
41 set compilable_output_file_ext "h"
42 lappend out "-fdump-c++-spec=[file rootname $name].h"
43
44 } elseif [string match "-HC=verbose" $arg] {
45 lappend out "-fdump-c++-spec-verbose"
46
47 } elseif { [regexp -- {^-I([\w+/-]+)} $arg pattern path] } {
48 lappend out "-I$path"
49
50 } elseif { [regexp -- {^-J([\w+/-]+)} $arg pattern path] } {
51 lappend out "-J$path"
52
53 } elseif [string match "-X" $arg] {
54 upvar 1 compilable_output_file_ext compilable_output_file_ext
55 set compilable_output_file_ext "json"
56 lappend out "-X"
57
58 } elseif [string match "-allinst" $arg] {
59 lappend out "-fall-instantiations"
60
61 } elseif [string match "-betterC" $arg] {
62 lappend out "-fno-druntime"
63
64 } elseif { [string match "-boundscheck" $arg]
65 || [string match "-boundscheck=on" $arg] } {
66 lappend out "-fbounds-check=on"
67
68 } elseif { [string match "-boundscheck=off" $arg]
69 || [string match "-noboundscheck" $arg] } {
70 lappend out "-fbounds-check=off"
71
72 } elseif [string match "-boundscheck=safeonly" $arg] {
73 lappend out "-fbounds-check=safeonly"
74
75 } elseif [string match "-c" $arg] {
76 lappend out "-c"
77
78 } elseif [regexp -- {^-check=(\w+)=off} $arg pattern value] {
79 lappend out "-fno-check=$value"
80
81 } elseif [string match "-checkaction=context" $arg] {
82 lappend out "-fcheckaction=context"
83
84 } elseif [string match "-d" $arg] {
85 lappend out "-Wno-deprecated"
86
87 } elseif [string match "-de" $arg] {
88 lappend out "-Wdeprecated"
89 lappend out "-Werror"
90
91 } elseif [string match "-debug" $arg] {
92 lappend out "-fdebug"
93
94 } elseif [regexp -- {^-debug=(\w+)} $arg pattern value] {
95 lappend out "-fdebug=$value"
96
97 } elseif [string match "-dip25" $arg] {
98 lappend out "-fpreview=dip25"
99
100 } elseif [string match "-dip1000" $arg] {
101 lappend out "-fpreview=dip1000"
102
103 } elseif [string match "-dip1008" $arg] {
104 lappend out "-fpreview=dip1008"
105
106 } elseif [string match "-dw" $arg] {
107 lappend out "-Wdeprecated"
108 lappend out "-Wno-error"
109
110 } elseif [regexp -- {^-extern-std=([\w+]+)} $arg pattern value] {
111 lappend out "-fextern-std=$value"
112
113 } elseif [string match "-fPIC" $arg] {
114 lappend out "-fPIC"
115
116 } elseif [string match "-fPIE" $arg] {
117 lappend out "-fPIE"
118
119 } elseif { [string match "-g" $arg]
120 || [string match "-gc" $arg] } {
121 lappend out "-g"
122
123 } elseif [string match "-ignore" $arg] {
124 lappend out "-fignore-unknown-pragmas"
125
126 } elseif [string match "-inline" $arg] {
127 lappend out "-finline-functions"
128
129 } elseif [string match "-main" $arg] {
130 lappend out "-fmain"
131
132 } elseif [regexp -- {^-mv=([\w+=./-]+)} $arg pattern value] {
133 lappend out "-fmodule-file=$value"
134
135 } elseif [string match "-O" $arg] {
136 lappend out "-O2"
137
138 } elseif [regexp -- {^-preview=(\w+)} $arg pattern value] {
139 lappend out "-fpreview=[string tolower $value]"
140
141 } elseif [string match "-release" $arg] {
142 lappend out "-frelease"
143
144 } elseif [regexp -- {^-revert=(\w+)} $arg pattern value] {
145 lappend out "-frevert=[string tolower $value]"
146
147 } elseif [regexp -- {^-transition=(\w+)} $arg pattern value] {
148 lappend out "-ftransition=[string tolower $value]"
149
150 } elseif [string match "-unittest" $arg] {
151 lappend out "-funittest"
152
153 } elseif [string match "-verrors=spec" $arg] {
154 lappend out "-Wspeculative"
155
156 } elseif [regexp -- {^-verrors=(\d+)} $arg pattern num] {
157 lappend out "-fmax-errors=$num"
158
159 } elseif [regexp -- {^-version=(\w+)} $arg pattern value] {
160 lappend out "-fversion=$value"
161
162 } elseif [string match "-o-" $arg] {
163 upvar 2 compilable_do_what compilable_do_what
164 set compilable_do_what "compile"
165
166 } elseif [string match "-nothrow" $arg] {
167 lappend out "-fno-exceptions"
168
169 } elseif [string match "-vgc" $arg] {
170 lappend out "-ftransition=nogc"
171
172 } elseif [string match "-vtemplates" $arg] {
173 lappend out "-ftransition=templates"
174
175 } elseif [string match "-vtls" $arg] {
176 lappend out "-ftransition=tls"
177
178 } elseif [string match "-w" $arg] {
179 lappend out "-Wall"
180 lappend out "-Werror"
181
182 } elseif [string match "-wi" $arg] {
183 lappend out "-Wall"
184 lappend out "-Wno-error"
185
186 } else {
187 # print "Unhandled Argument: $arg"
188 }
189 }
190
191 return $out
192 }
193
194 proc gdc-copy-file { srcdir filename } {
195 # Split folder/file from the filename.
196 set targetdir [file dirname $filename]
197
198 # print "Filename: $srcdir - $filename"
199
200 file mkdir $targetdir
201 file copy -force $srcdir/$filename $filename
202
203 # Remove file once test is finished.
204 upvar 2 cleanup_extra_files cleanups
205 lappend cleanups $filename
206 }
207
208 #
209 # Translate DMD test directives to dejagnu equivalent.
210 #
211 # This procedure copies the test and all its dependencies from its source
212 # location in `$srcdir/$type' to `$base_dir/$type'. A stub dejagnu test file
213 # is then created in `$base_dir/gdc.test/$type' containing all translated test
214 # directives. This stub is then the test that is handed over to `dg-test'.
215 # Before invoking the compiler, the `gdc.test' prefix is trimmed from the test
216 # `$prog' name in `gdc-dg-test' so that all copied tests then get picked up
217 # with the correct path names.
218 #
219 # The following directives are recognized:
220 #
221 # COMPILE_SEPARATELY: Not handled.
222 # EXECUTE_ARGS: Parameters to add to the execution of the test.
223 # COMPILED_IMPORTS: List of modules files that are imported by the main
224 # source file that should be included in compilation.
225 # Currently handled the same as EXTRA_SOURCES.
226 # DFLAGS: Overrides the DFLAGS environment variable if specified
227 # in the test. No values are permitted; an error will be
228 # emitted if the value is not empty.
229 # EXTRA_SOURCES: List of extra sources to build and link along with
230 # the test.
231 # EXTRA_CPP_SOURCES: List of extra C++ files to build and link along with
232 # the test.
233 # CXXFLAGS: list of extra arguments passed when compiling C++
234 # sources defined in EXTRA_CPP_SOURCES.
235 # EXTRA_OBJC_SOURCES: List of extra Objective-C file to build and link along
236 # with the test. Currently not handled.
237 # EXTRA_FILES: List of extra files to copy for the test runs.
238 # PERMUTE_ARGS: The set of arguments to permute in multiple compiler
239 # invocations. An empty set means only one permutation
240 # with no arguments.
241 # ARG_SETS: Not handled.
242 # LINK: Enables linking.
243 # TEST_OUTPUT: The output expected from the compilation.
244 # POST_SCRIPT: Not handled.
245 # REQUIRED_ARGS: Arguments to add to the compiler command line.
246 # DISABLED: Not handled.
247 # UNICODE_NAMES: Requires ucn support.
248 #
249
250 proc gdc-convert-test { base test } {
251 global DEFAULT_DFLAGS
252 global PERMUTE_ARGS
253 global GDC_EXECUTE_ARGS
254
255 set PERMUTE_ARGS ""
256 set GDC_EXECUTE_ARGS ""
257
258 set extra_sources ""
259 set extra_files ""
260 set needs_phobos 0
261 set saw_test_flags 0
262
263 upvar 1 compilable_do_what compilable_do_what
264 set compilable_output_file_ext ""
265
266 # Split folder/file from the test.
267 set type [file dirname $test]
268 set name [file tail $test]
269
270 # print "Filename: $base - $test"
271 gdc-copy-file $base $test
272
273 # Read in all test directives, and save the dejagnu equivalents to a new
274 # file that will be returned as the test to run.
275 set fdin [open $base/$test r]
276 #fconfigure $fdin -encoding binary
277
278 # Include gdc.test prefix so test names follow DejaGnu conventions.
279 set testdir [file tail $base]
280 file mkdir $testdir/$type
281 set fdout [open $testdir/$test w]
282 #fconfigure $fdout -encoding binary
283
284 while { [gets $fdin copy_line] >= 0 } {
285
286 if [regexp -- {COMPILE_SEPARATELY} $copy_line] {
287 # COMPILE_SEPARATELY is not handled.
288
289 } elseif [regexp -- {DISABLED} $copy_line] {
290 # DISABLED is not handled.
291
292 } elseif [regexp -- {LINK:} $copy_line] {
293 # LINK sets dg-do-what-default "link"
294 set compilable_do_what "link"
295
296 } elseif [regexp -- {ARG_SETS} $copy_line] {
297 # ARG_SETS is not handled.
298 regsub -- {ARG_SETS.*$} $copy_line "" out_line
299
300 } elseif [regexp -- {POST_SCRIPT} $copy_line] {
301 # POST_SCRIPT is not handled
302
303 } elseif [regexp -- {DFLAGS\s*:\s*(.*)} $copy_line match args] {
304 # DFLAGS overrides the default value of PERMUTE_ARGS.
305 if { $args != "" } {
306 error "gdc-convert-test: DFLAGS is not empty as expected"
307 }
308 set saw_test_flags 1
309
310 } elseif [regexp -- {PERMUTE_ARGS\s*:\s*(.*)} $copy_line match args] {
311 # PERMUTE_ARGS is handled by gdc-do-test.
312 set PERMUTE_ARGS [gdc-convert-args $args]
313 set saw_test_flags 1
314
315 } elseif [regexp -- {EXECUTE_ARGS\s*:\s*(.*)} $copy_line match args] {
316 # EXECUTE_ARGS is handled by gdc_load.
317 foreach arg $args {
318 lappend GDC_EXECUTE_ARGS $arg
319 }
320
321 } elseif [regexp -- {REQUIRED_ARGS\s*:\s*(.*)} $copy_line match args] {
322 # Convert all listed arguments to from dmd to gdc-style.
323 puts $fdout "// { dg-additional-options \"[gdc-convert-args $args]\" }"
324
325 } elseif [regexp -- {EXTRA_SOURCES\s*:\s*(.*)} $copy_line match sources] {
326 # EXTRA_SOURCES are appended to extra_sources list
327 foreach srcfile $sources {
328 lappend extra_sources $srcfile
329 }
330
331 } elseif [regexp -- {EXTRA_CPP_SOURCES\s*:\s*(.*)} $copy_line match sources] {
332 # EXTRA_CPP_SOURCES are appended to extra_sources list
333 foreach srcfile $sources {
334 # C++ sources are found in the extra-files directory.
335 lappend extra_sources "extra-files/$srcfile"
336 }
337
338 } elseif [regexp -- {CXXFLAGS\s*:\s*(.*)} $copy_line match args] {
339 # Both C++ and D sources are compiled together, so include each
340 # other's command line flags too.
341 set new_option "{ dg-additional-options \"$args\" }"
342 regsub -- {CXXFLAGS.*$} $copy_line $new_option out_line
343
344 } elseif [regexp -- {EXTRA_OBJC_SOURCES\s*:\s*(.*)} $copy_line match sources] {
345 # EXTRA_OBJC_SOURCES is not handled.
346 regsub -- {EXTRA_OBJC_SOURCES.*$} $copy_line "" out_line
347
348 } elseif [regexp -- {EXTRA_FILES\s*:\s*(.*)} $copy_line match files] {
349 # EXTRA_FILES are appended to extra_files list
350 foreach file $files {
351 lappend extra_files $file
352 }
353
354 } elseif [regexp -- {COMPILED_IMPORTS\s*:\s*(.*)} $copy_line match sources] {
355 # COMPILED_IMPORTS are appended to extra_sources list
356 foreach import $sources {
357 lappend extra_sources $import
358 }
359
360 } elseif [regexp -- {RUNNABLE_PHOBOS_TEST} $copy_line match sources] {
361 # RUNNABLE_PHOBOS_TEST annotates tests that import the std module.
362 # It will need skipping if phobos is not available on the target.
363 set needs_phobos 1
364
365 } elseif [regexp -- {COMPILABLE_MATH_TEST} $copy_line match sources] {
366 # COMPILABLE_MATH_TEST annotates tests that import the std.math
367 # module. Which will need skipping if not available on the target.
368 set needs_phobos 1
369 } elseif [regexp -- {UNICODE_NAMES} $copy_line] {
370 # Require ucn support.
371 puts $fdout "// { dg-require-effective-target ucn }"
372
373 }
374 }
375
376 # Now that all extra sources and files have been collected, copy them all
377 # to the testsuite build directory.
378 if { [llength $extra_sources] > 0 } {
379 foreach srcfile $extra_sources {
380 gdc-copy-file $base "$type/$srcfile"
381 }
382 puts $fdout "// { dg-additional-sources \"$extra_sources\" }"
383 }
384
385 if { [llength $extra_files] > 0 } {
386 foreach file $extra_files {
387 gdc-copy-file $base "$type/$file"
388 }
389 puts $fdout "// { dg-additional-files \"$extra_files\" }"
390 }
391
392 # Add specific options for test type
393
394 # DMD's testsuite is extremely verbose, compiler messages from constructs
395 # such as pragma(msg, ...) would otherwise cause tests to fail.
396 puts $fdout "// { dg-prune-output .* }"
397
398 # Compilable files are successful if an output is generated.
399 # Fail compilable are successful if an output is not generated.
400 # Runnable must compile, link, and return 0 to be successful by default.
401 switch $type {
402 runnable_cxx -
403 runnable {
404 if ![isnative] {
405 puts $fdout "// { dg-final { output-exists } }"
406 }
407 if $needs_phobos {
408 puts $fdout "// { dg-skip-if \"imports phobos\" { ! d_runtime_has_std_library } }"
409 }
410 # Run runnable tests with all default permutations if not
411 # explicitly set in the test file.
412 if !$saw_test_flags {
413 set PERMUTE_ARGS $DEFAULT_DFLAGS
414 }
415 }
416
417 compilable {
418 puts $fdout "// { dg-final { output-exists } }"
419
420 # Compilable test may require checking another kind of output file.
421 if { $compilable_output_file_ext != "" } {
422 # Check that file generation tests output the expected file.
423 set genfile "[file rootname $name].$compilable_output_file_ext"
424 puts $fdout "// { dg-final { if \[file exists $genfile\] \\{ } }"
425 puts $fdout "// { dg-final { pass \"$testdir/$test (file exists $genfile)\" } }"
426 puts $fdout "// { dg-final { \\} else \\{ } }"
427 puts $fdout "// { dg-final { fail \"$testdir/$test (file exists $genfile)\" } }"
428 puts $fdout "// { dg-final { \\} } }"
429 # Cleanup extra generated files.
430 puts $fdout "// { dg-final { file delete $genfile } }"
431 }
432 if $needs_phobos {
433 puts $fdout "// { dg-skip-if \"imports phobos\" { ! d_runtime_has_std_library } }"
434 }
435 }
436
437 fail_compilation {
438 # Fail compilation tests only check for language errors from the
439 # front-end. No need to run all permutations of the default DFLAGS.
440 if { $PERMUTE_ARGS == $DEFAULT_DFLAGS } {
441 set PERMUTE_ARGS ""
442 }
443 puts $fdout "// { dg-final { output-exists-not } }"
444 }
445 }
446
447 close $fdin
448 close $fdout
449
450 return $testdir/$test
451 }
452
453 proc gdc-permute-options { options } {
454 set result { }
455 set n [expr 1<<[llength $options]]
456 for { set i 0 } { $i<$n } { incr i } {
457 set option ""
458 for { set j 0 } { $j<[llength $options] } { incr j } {
459 if [expr $i & 1 << $j] {
460 append option [lindex $options $j]
461 append option " "
462 }
463 }
464 lappend result $option
465
466 }
467 return $result
468 }
469
470 #
471 # Main loop for running all tests for the subdirectory in gdc.test
472 #
473
474 proc gdc-do-test { testcases } {
475 global dg-do-what-default
476 global verbose
477
478 # If a testcase doesn't have special options, use these.
479 global DEFAULT_DFLAGS
480 if ![info exists DEFAULT_DFLAGS] then {
481 set DEFAULT_DFLAGS "-g -O2 -frelease"
482 #set DEFAULT_DFLAGS "-O2"
483 }
484
485 # These are special options to use on testcase, and override DEFAULT_DFLAGS
486 global PERMUTE_ARGS
487
488 # Set if an extra option should be passed to link to shared druntime.
489 global SHARED_OPTION
490
491 # Additional arguments for gdc_load
492 global GDC_EXECUTE_ARGS
493
494 # Allow blank linkes in output for all of gdc.test.
495 global allow_blank_lines
496 set save_allow_blank_lines $allow_blank_lines
497 if { !$allow_blank_lines } {
498 set allow_blank_lines 2
499 }
500
501 set saved-dg-do-what-default ${dg-do-what-default}
502
503 # Main loop.
504
505 # set verbose 1
506 # set dg-final-code ""
507 # Find all tests and pass to routine.
508 foreach test $testcases {
509 regexp -- "(.*)/(.+)/(.+)\.(.+)$" $test match base type name ext
510
511 # Convert to DG test.
512 set imports [format "-I%s" $type]
513 set cleanup_extra_files ""
514 set compilable_do_what "compile"
515 set filename "[gdc-convert-test $base $type/$name.$ext]"
516
517 if { $type == "runnable" || $type == "runnable_cxx" } {
518 append PERMUTE_ARGS " $SHARED_OPTION"
519 }
520
521 set options [gdc-permute-options [lsort -unique $PERMUTE_ARGS]]
522
523 switch $type {
524 runnable_cxx -
525 runnable {
526 for { set i 0 } { $i<[llength $options] } { incr i } {
527 set flags [lindex $options $i]
528 if [isnative] {
529 set dg-do-what-default "run"
530 } else {
531 set dg-do-what-default "link"
532 }
533 gdc-dg-runtest $filename $flags $imports
534 }
535 }
536
537 compilable {
538 for { set i 0 } { $i<[llength $options] } { incr i } {
539 set flags [lindex $options $i]
540 set dg-do-what-default $compilable_do_what
541 gdc-dg-runtest $filename $flags $imports
542 }
543 }
544
545 fail_compilation {
546 for { set i 0 } { $i<[llength $options] } { incr i } {
547 set flags [lindex $options $i]
548 set dg-do-what-default "assemble"
549 gdc-dg-runtest $filename $flags $imports
550 }
551 }
552 }
553
554 # Cleanup test directory.
555 foreach srcfile $cleanup_extra_files {
556 file delete $srcfile
557 }
558 file delete $filename
559 }
560
561 set dg-do-what-default ${saved-dg-do-what-default}
562 set allow_blank_lines $save_allow_blank_lines
563 }