Generate and scan documentation output in Ddoc tests.
[official-gcc.git] / gcc / testsuite / gdc.test / gdc-test.exp
blob7dd97d393ea0911e9666d3d338004f7773efaff1
1 # Copyright (C) 2012-2018 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 GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
17 # Test using the DMD testsuite.
18 # Load support procs.
19 load_lib gdc-dg.exp
22 # Convert DMD arguments to GDC equivalent
25 proc gdc-convert-args { args } {
26 set out ""
28 foreach arg [split [lindex $args 0] " "] {
29 # List of switches kept in ASCII collated order.
30 if [string match "-D" $arg] {
31 lappend out "-fdoc"
33 } elseif { [regexp -- {^-I([\w+/-]+)} $arg pattern path] } {
34 lappend out "-I$path"
36 } elseif { [regexp -- {^-J([\w+/-]+)} $arg pattern path] } {
37 lappend out "-J$path"
39 } elseif [string match "-allinst" $arg] {
40 lappend out "-fall-instantiations"
42 } elseif { [string match "-boundscheck" $arg]
43 || [string match "-boundscheck=on" $arg] } {
44 lappend out "-fbounds-check"
46 } elseif { [string match "-boundscheck=off" $arg]
47 || [string match "-noboundscheck" $arg] } {
48 lappend out "-fno-bounds-check"
50 } elseif [string match "-boundscheck=safeonly" $arg] {
51 lappend out "-fbounds-check=safeonly"
53 } elseif [string match "-c" $arg] {
54 lappend out "-c"
56 } elseif [string match "-d" $arg] {
57 lappend out "-Wno-deprecated"
59 } elseif [string match "-de" $arg] {
60 lappend out "-Wdeprecated"
61 lappend out "-Werror"
63 } elseif [string match "-debug" $arg] {
64 lappend out "-fdebug"
66 } elseif [regexp -- {^-debug=(\w+)} $arg pattern value] {
67 lappend out "-fdebug=$value"
69 } elseif [string match "-dip1000" $arg] {
70 lappend out "-ftransition=dip1000"
72 } elseif [string match "-dip25" $arg] {
73 lappend out "-ftransition=dip25"
75 } elseif [string match "-dw" $arg] {
76 lappend out "-Wdeprecated"
77 lappend out "-Wno-error"
79 } elseif [string match "-fPIC" $arg] {
80 lappend out "-fPIC"
82 } elseif { [string match "-g" $arg]
83 || [string match "-gc" $arg] } {
84 lappend out "-g"
86 } elseif [string match "-inline" $arg] {
87 lappend out "-finline-functions"
89 } elseif [string match "-main" $arg] {
90 lappend out "-fmain"
92 } elseif [regexp -- {^-mv=([\w+=./-]+)} $arg pattern value] {
93 lappend out "-fmodule-file=$value"
95 } elseif [string match "-O" $arg] {
96 lappend out "-O2"
98 } elseif [string match "-release" $arg] {
99 lappend out "-frelease"
101 } elseif [regexp -- {^-transition=(\w+)} $arg pattern value] {
102 lappend out "-ftransition=$value"
104 } elseif [string match "-unittest" $arg] {
105 lappend out "-funittest"
107 } elseif [string match "-verrors=spec" $arg] {
108 lappend out "-Wspeculative"
110 } elseif [regexp -- {^-verrors=(\d+)} $arg pattern num] {
111 lappend out "-fmax-errors=$num"
113 } elseif [regexp -- {^-version=(\w+)} $arg pattern value] {
114 lappend out "-fversion=$value"
116 } elseif [string match "-vtls" $arg] {
117 lappend out "-ftransition=tls"
119 } elseif [string match "-w" $arg] {
120 lappend out "-Wall"
121 lappend out "-Werror"
123 } elseif [string match "-wi" $arg] {
124 lappend out "-Wall"
125 lappend out "-Wno-error"
127 } else {
128 # print "Unhandled Argument: $arg"
132 return $out
135 proc gdc-copy-extra { base extra } {
136 # Split base, folder/file.
137 set type [file dirname $extra]
139 # print "Filename: $base - $extra"
141 set fdin [open $base/$extra r]
142 fconfigure $fdin -encoding binary
144 file mkdir $type
145 set fdout [open $extra w]
146 fconfigure $fdout -encoding binary
148 while { [gets $fdin copy_line] >= 0 } {
149 set out_line $copy_line
150 puts $fdout $out_line
153 close $fdin
154 close $fdout
156 return $extra
160 # Translate DMD test directives to dejagnu equivalent.
162 # COMPILE_SEPARATELY: Not handled.
163 # EXECUTE_ARGS: Parameters to add to the execution of the test.
164 # COMPILED_IMPORTS: List of modules files that are imported by the main
165 # source file that should be included in compilation.
166 # Currently handled the same as EXTRA_SOURCES.
167 # EXTRA_SOURCES: List of extra sources to build and link along with
168 # the test.
169 # EXTRA_FILES: List of extra files to copy for the test runs.
170 # PERMUTE_ARGS: The set of arguments to permute in multiple compiler
171 # invocations. An empty set means only one permutation
172 # with no arguments.
173 # TEST_OUTPUT: The output expected from the compilation.
174 # POST_SCRIPT: Not handled.
175 # REQUIRED_ARGS: Arguments to add to the compiler command line.
176 # DISABLED: Not handled.
179 proc dmd2dg { base test } {
180 global DEFAULT_DFLAGS
181 global PERMUTE_ARGS
182 global GDC_EXECUTE_ARGS
184 set PERMUTE_ARGS $DEFAULT_DFLAGS
185 set GDC_EXECUTE_ARGS ""
187 # Split base, folder/file.
188 set type [file dirname $test]
189 set name [file tail $test]
191 # print "Filename: $base - $test"
193 set fdin [open $base/$test r]
194 #fconfigure $fdin -encoding binary
196 file mkdir $type
197 set fdout [open $test w]
198 #fconfigure $fdout -encoding binary
200 while { [gets $fdin copy_line] >= 0 } {
201 set out_line $copy_line
203 if [regexp -- {COMPILE_SEPARATELY} $copy_line] {
204 # COMPILE_SEPARATELY is not handled.
205 regsub -- {COMPILE_SEPARATELY.*$} $copy_line "" out_line
207 } elseif [regexp -- {DISABLED} $copy_line] {
208 # DISABLED is not handled.
209 regsub -- {DISABLED.*$} $copy_line "" out_line
211 } elseif [regexp -- {POST_SCRIPT} $copy_line] {
212 # POST_SCRIPT is not handled
213 regsub -- {POST_SCRIPT.*$} $copy_line "" out_line
215 } elseif [regexp -- {PERMUTE_ARGS\s*:\s*(.*)} $copy_line match args] {
216 # PERMUTE_ARGS is handled by gdc-do-test.
217 set PERMUTE_ARGS [gdc-convert-args $args]
218 regsub -- {PERMUTE_ARGS.*$} $copy_line "" out_line
220 } elseif [regexp -- {EXECUTE_ARGS\s*:\s*(.*)} $copy_line match args] {
221 # EXECUTE_ARGS is handled by gdc_load.
222 foreach arg $args {
223 lappend GDC_EXECUTE_ARGS $arg
225 regsub -- {EXECUTE_ARGS.*$} $copy_line "" out_line
227 } elseif [regexp -- {REQUIRED_ARGS\s*:\s*(.*)} $copy_line match args] {
228 # Convert all listed arguments to from dmd to gdc-style.
229 set new_option "{ dg-additional-options \"[gdc-convert-args $args]\" }"
230 regsub -- {REQUIRED_ARGS.*$} $copy_line $new_option out_line
232 } elseif [regexp -- {EXTRA_SOURCES\s*:\s*(.*)} $copy_line match sources] {
233 # Copy all sources to the testsuite build directory.
234 foreach import $sources {
235 # print "Import: $base $type/$import"
236 gdc-copy-extra $base "$type/$import"
238 set new_option "{ dg-additional-sources \"$sources\" }"
239 regsub -- {EXTRA_SOURCES.*$} $copy_line $new_option out_line
241 } elseif [regexp -- {EXTRA_CPP_SOURCES\s*:\s*(.*)} $copy_line match sources] {
242 # Copy all sources to the testsuite build directory.
243 foreach import $sources {
244 # print "Import: $base $type/$import"
245 gdc-copy-extra $base "$type/$import"
247 set new_option "{ dg-additional-sources \"$sources\" }"
248 regsub -- {EXTRA_CPP_SOURCES.*$} $copy_line $new_option out_line
250 } elseif [regexp -- {EXTRA_FILES\s*:\s*(.*)} $copy_line match files] {
251 # Copy all files to the testsuite build directory.
252 foreach import $files {
253 # print "Import: $base $type/$import"
254 gdc-copy-extra $base "$type/$import"
256 set new_option "{ dg-additional-files \"$files\" }"
257 regsub -- {EXTRA_FILES.*$} $copy_line $new_option out_line
259 } elseif [regexp -- {COMPILED_IMPORTS\s*:\s*(.*)} $copy_line match sources] {
260 # Copy all sources to the testsuite build directory.
261 foreach import $sources {
262 # print "Import: $base $type/$import"
263 gdc-copy-extra $base "$type/$import"
265 set new_option "{ dg-additional-sources \"$sources\" }"
266 regsub -- {COMPILED_IMPORTS.*$} $copy_line $new_option out_line
270 puts $fdout $out_line
273 # Add specific options for test type
275 # DMD's testsuite is extremely verbose, compiler messages from constructs
276 # such as pragma(msg, ...) would otherwise cause tests to fail.
277 set out_line "// { dg-prune-output .* }"
278 puts $fdout $out_line
280 # Since GCC 6-20160131 blank lines are not allowed in the output by default.
281 dg-allow-blank-lines-in-output { 1 }
283 # Compilable files are successful if an output is generated.
284 # Fail compilable are successful if an output is not generated.
285 # Runnable must compile, link, and return 0 to be successful by default.
286 switch $type {
287 runnable {
288 if ![isnative] {
289 set out_line "// { dg-final { output-exists } }"
290 puts $fdout $out_line
294 compilable {
295 set out_line "// { dg-final { output-exists } }"
296 puts $fdout $out_line
298 # Check that Ddoc tests also generate a html file.
299 if [regexp -- "ddoc.*" $name] {
300 set ddocfile "[file rootname $name].html"
301 set out_line "// { dg-final { scan-file $ddocfile \"Generated by Ddoc from $test\" } }"
302 puts $fdout $out_line
303 # Cleanup extra generated files.
304 set out_line "// { dg-final { file delete $ddocfile } }"
305 puts $fdout $out_line
309 fail_compilation {
310 set out_line "// { dg-final { output-exists-not } }"
311 puts $fdout $out_line
315 close $fdin
316 close $fdout
318 return $test
321 proc gdc-permute-options { options } {
322 set result { }
323 set n [expr 1<<[llength $options]]
324 for { set i 0 } { $i<$n } { incr i } {
325 set option ""
326 for { set j 0 } { $j<[llength $options] } { incr j } {
327 if [expr $i & 1 << $j] {
328 append option [lindex $options $j]
329 append option " "
332 lappend result $option
335 return $result
339 proc gdc-do-test { } {
340 global srcdir subdir
341 global dg-do-what-default
342 global verbose
344 # If a testcase doesn't have special options, use these.
345 global DEFAULT_DFLAGS
346 if ![info exists DEFAULT_DFLAGS] then {
347 set DEFAULT_DFLAGS "-g -O2 -frelease"
348 #set DEFAULT_DFLAGS "-O2"
351 # These are special options to use on testcase, and override DEFAULT_DFLAGS
352 global PERMUTE_ARGS
354 # Set if an extra option should be passed to link to shared druntime.
355 global SHARED_OPTION
357 # Additional arguments for gdc_load
358 global GDC_EXECUTE_ARGS
360 # Initialize `dg'.
361 dg-init
363 # Main loop.
365 # set verbose 1
366 # set dg-final-code ""
367 # Find all tests and pass to routine.
368 foreach test [lsort [find $srcdir/$subdir *]] {
369 regexp -- "(.*)/(.+)/(.+)\.(.+)$" $test match base dir name ext
371 # Skip invalid test directory
372 if { [lsearch "runnable compilable fail_compilation" $dir] == -1 } {
373 continue
376 # Skip invalid test extensions
377 if { [lsearch "d" $ext] == -1 } {
378 continue
381 # Convert to DG test.
382 set imports [format "-I%s/%s" $base $dir]
383 set filename [dmd2dg $base $dir/$name.$ext]
385 if { $dir == "runnable" } {
386 append PERMUTE_ARGS " $SHARED_OPTION"
388 set options [gdc-permute-options $PERMUTE_ARGS]
390 switch $dir {
391 runnable {
392 for { set i 0 } { $i<[llength $options] } { incr i } {
393 set flags [lindex $options $i]
394 if [isnative] {
395 set dg-do-what-default "run"
396 } else {
397 set dg-do-what-default "link"
399 gdc-dg-runtest $filename $flags $imports
403 compilable {
404 for { set i 0 } { $i<[llength $options] } { incr i } {
405 set flags [lindex $options $i]
406 # Compilable test may require checking another kind of output file.
407 if [regexp -- "ddoc.*" $name] {
408 set dg-do-what-default "compile"
409 } else {
410 set dg-do-what-default "assemble"
412 gdc-dg-runtest $filename $flags $imports
416 fail_compilation {
417 for { set i 0 } { $i<[llength $options] } { incr i } {
418 set flags [lindex $options $i]
419 set dg-do-what-default "assemble"
420 gdc-dg-runtest $filename $flags $imports
425 # Cleanup
426 #file delete $filename
429 # All done.
430 dg-finish
433 gdc-do-test