Debian Codegraph @

gcc/testsuite/g++.dg/modules/modules.exp

raw
1 # Copyright (C) 2017-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 # Contributed by Nathan Sidwell <[email protected]> while at Facebook
18
19
20 # Test C++ modules, which requires multiple TUs
21 #
22 # A test case might consist of multiple source files, each is compiled
23 # separately, in a well-defined order. The resulting object files might
24 # be optionally linked and optionally executed. Grouping is indicated by
25 # naming files '*_[a-z].[CH]'
26
27 # { dg-module-cmi "[!]module-name" } # an interface file is (not) expected
28 # { dg-module-do [link|run] [xfail] [options] } # link [and run]
29
30 load_lib g++-dg.exp
31 load_lib modules.exp
32
33 # If a testcase doesn't have special options, use these.
34 global DEFAULT_CXXFLAGS
35 if ![info exists DEFAULT_CXXFLAGS] then {
36 set DEFAULT_CXXFLAGS " -pedantic-errors -Wno-long-long"
37 }
38 set DEFAULT_MODFLAGS $DEFAULT_CXXFLAGS
39 set MOD_STD_LIST { 17 2a 2b }
40
41 dg-init
42
43 if {[is_remote host]} {
44 # remote testing not functional here :(
45 return
46 }
47
48 if { [istarget "powerpc-ibm-aix*"] } {
49 set torture_execute_xfail "powerpc-ibm-aix*"
50 return
51 }
52
53 global module_do
54 global module_cmis
55
56 set DEFAULT_REPO "gcm.cache"
57
58 # Register the module name this produces.
59 # dg-module-cmi !?=?NAME WHEN?
60 # dg-module-cmi !?{} - header unit
61 proc dg-module-cmi { args } {
62 if { [llength $args] > 3 } {
63 error "[lindex $args 0]: too many arguments"
64 return
65 }
66 set spec [lindex $args 1]
67 if { [llength $args] > 2 } {
68 set when [lindex $args 2]
69 } else {
70 set when {}
71 }
72
73 if { [string index $spec 0] == "!" } {
74 set name [string range $spec 1 end]
75 set not 1
76 } else {
77 set name $spec
78 set not 0
79 }
80
81 if { [string index $name 0] == "=" } {
82 set cmi [string range $name 1 end]
83 } else {
84 if { $name == "" } {
85 # get the source file name. ick!
86 upvar prog srcname
87 set cmi "$srcname.gcm"
88 if { [string index $cmi 0] == "/" } {
89 set cmi [string range $cmi 1 end]
90 } else {
91 set cmi ",/$cmi"
92 }
93 set path [file split $cmi]
94 # subst /../ -> /,,/
95 # sadly tcl 8.5 does not have lmap
96 set rplac {}
97 foreach elt $path {lappend rplac [expr {$elt == ".." ? ",," : $elt}]}
98 set cmi [file join {*}$rplac]
99 } else {
100 set cmi "[regsub : $name -].gcm"
101 }
102 global DEFAULT_REPO
103 set cmi "$DEFAULT_REPO/$cmi"
104 }
105
106 # delete file, so we don't get confused by a stale one.
107 file_on_host delete "$cmi"
108
109 global module_cmis
110 lappend module_cmis [list $spec $when $not $cmi]
111 }
112
113 # check the expected module files exist (or not)
114 # return list to delete
115 proc module_cmi_p { src ifs } {
116 set res {}
117 foreach if_arg $ifs {
118 set spec [lindex $if_arg 0]
119 set when [lindex $if_arg 1]
120 if { $when != "" } {
121 switch [dg-process-target $when] {
122 "S" { }
123 "N" { continue }
124 "F" { setup_xfail "*-*-*" }
125 "P" { }
126 }
127 }
128 set not [lindex $if_arg 2]
129 set cmi [lindex $if_arg 3]
130 global srcdir
131 set relcmi [string map [list $srcdir "/\$srcdir"] $cmi]
132 if { $not != [file_on_host exists $cmi] } {
133 pass "$src module-cmi $spec ($relcmi)"
134 } else {
135 fail "$src module-cmi $spec ($relcmi)"
136 set not [expr ! $not ]
137 }
138 if { ! $not } {
139 lappend res $cmi
140 }
141 }
142 return $res
143 }
144
145 # link and maybe run a set of object files
146 # dg-module-do WHAT WHEN
147 proc dg-module-do { args } {
148 if { [llength $args] > 3 } {
149 error "[lindex $args 0]: too many arguments"
150 return
151 }
152
153 set do_what [lindex $args 1]
154 set expected "P"
155 if { [llength $args] > 2 } {
156 set expected [dg-process-target [lindex $args 2]]
157 }
158
159 global module_do
160 set module_do [list $do_what $expected]
161 }
162
163 proc module_do_it { do_what testcase std asm_list } {
164 global tool
165
166 set run 0
167 switch [lindex $do_what 0] {
168 "compile" { return 1 }
169 "link" { }
170 "run" { set run 1 }
171 default { error "unknown module-do action [lindex $do_what 0]" }
172 }
173
174 set xfail {}
175 switch [lindex $do_what 1] {
176 "S" { }
177 "N" { return 1 }
178 "F" { set xfail {setup_xfail "*-*-*"} }
179 "P" { }
180 }
181
182 set ok 1
183 # make sure all asms are around
184 foreach asm $asm_list {
185 if { ! [file_on_host exists $asm] } {
186 set ok 0
187 }
188 }
189
190 set options { }
191 set ident $testcase
192 if { $std != "" } {
193 lappend options "additional_flags=$std"
194 set ident "$ident $std"
195 }
196 global extra_tool_flags
197 if { [llength $extra_tool_flags] } {
198 lappend options "additional_flags=$extra_tool_flags"
199 }
200
201 set execname "./[file tail $testcase].exe"
202
203 # link it
204 verbose "Linking $asm_list" 1
205 if { !$ok } {
206 unresolved "$ident link"
207 } else {
208 global target_triplet
209 set out [${tool}_target_compile $asm_list \
210 $execname executable $options]
211 eval $xfail
212
213 # Do gcc-specific pruning.
214 set out [${tool}-dg-prune $target_triplet $out]
215 # Fix up remaining line-breaks similar to "regular" pruning
216 # calls. Otherwise, a multi-line message stripped e.g. one
217 # part by the default prune_warnings and one part part by the
218 # gcc prune_gcc_output will have a residual line-break.
219 regsub "^\[\r\n\]+" $out "" out
220
221 if { $out == "" } {
222 pass "$ident link"
223 } else {
224 fail "$ident link"
225 set ok 0
226 }
227 }
228
229 # run it?
230 if { !$run } {
231 } elseif { !$ok } {
232 unresolved "$ident execute"
233 } else {
234 set out [${tool}_load $execname "" ""]
235 set status [lindex $out 0]
236 eval $xfail
237 $status "$ident execute"
238 if { $status != "pass" } {
239 set $ok 0
240 }
241 }
242
243 if { $ok } {
244 file_on_host delete $execname
245 }
246
247 return $ok
248 }
249
250 # delete the specified set of module files
251 proc cleanup_module_files { files } {
252 foreach file $files {
253 file_on_host delete $file
254 }
255 }
256
257 global testdir
258 set testdir $srcdir/$subdir
259 proc srcdir {} {
260 global testdir
261 return $testdir
262 }
263
264 # Return set of std options to iterate over, taken from g++-dg.exp & compat.exp
265 proc module-init { src } {
266 set tmp [dg-get-options $src]
267 set option_list {}
268 set have_std 0
269 set std_prefix "-std=c++"
270 global extra_tool_flags
271 set extra_tool_flags {}
272 global MOD_STD_LIST
273
274 foreach op $tmp {
275 switch [lindex $op 0] {
276 "dg-options" {
277 set std_prefix "-std=gnu++"
278 if { [string match "*-std=*" [lindex $op 2]] } {
279 set have_std 1
280 }
281 eval lappend extra_tool_flags [lindex $op 2]
282 }
283 "dg-additional-options" {
284 if { [string match "*-std=*" [lindex $op 2]] } {
285 set have_std 1
286 }
287 eval lappend extra_tool_flags [lindex $op 2]
288 }
289 }
290 }
291
292 if { $have_std } {
293 lappend option_list ""
294 } elseif { [string match "*xtreme*" $src] } {
295 # Only run the xtreme tests once.
296 set x [lindex $MOD_STD_LIST end]
297 lappend option_list "${std_prefix}$x"
298 } else {
299 foreach x $MOD_STD_LIST {
300 lappend option_list "${std_prefix}$x"
301 }
302 }
303
304 return $option_list
305 }
306
307 # Return 1 if requirements are met
308 proc module-check-requirements { tests } {
309 foreach test $tests {
310 set tmp [dg-get-options $test]
311 foreach op $tmp {
312 switch [lindex $op 0] {
313 "dg-additional-options" {
314 # Example strings to match:
315 # -fmodules-ts -fmodule-mapper=|@g++-mapper-server\\ -t\\ [srcdir]/inc-xlate-1.map
316 # -fmodules-ts -fmodule-mapper=|@g++-mapper-server
317 if [regexp -- {(^| )-fmodule-mapper=\|@([^\\ ]*)} [lindex $op 2] dummy dummy2 prog] {
318 verbose "Checking that mapper exist: $prog"
319 if { ![ check_is_prog_name_available $prog ] } {
320 return 0
321 }
322 }
323 }
324 }
325 }
326 }
327 return 1
328 }
329
330 # cleanup any detritus from previous run
331 cleanup_module_files [find $DEFAULT_REPO *.gcm]
332
333 # Override unsupported to set the second element of module_do to "N",
334 # so that, after an unsupported result in dg-test, we can skip rather
335 # than fail subsequent related tests.
336 set module_do {"compile" "P"}
337 rename unsupported modules-saved-unsupported
338 proc unsupported { args } {
339 global module_do
340 lset module_do 1 "N"
341 return [uplevel 1 modules-saved-unsupported $args]
342 }
343
344 # not grouped tests, sadly tcl doesn't have negated glob
345 foreach test [prune [lsort [find $srcdir/$subdir {*.[CH]}]] \
346 "$srcdir/$subdir/*_?.\[CH\]"] {
347 if [runtest_file_p $runtests $test] {
348 set nshort [file tail [file dirname $test]]/[file tail $test]
349
350 set std_list [module-init $test]
351 foreach std $std_list {
352 global module_cmis
353 set module_cmis {}
354 verbose "Testing $nshort $std" 1
355 dg-test $test "$std" $DEFAULT_MODFLAGS
356 if { [lindex $module_do 1] == "N" } {
357 continue
358 }
359 set testcase [string range $test [string length "$srcdir/"] end]
360 cleanup_module_files [module_cmi_p $testcase $module_cmis]
361 }
362 }
363 }
364
365 # grouped tests
366 foreach src [lsort [find $srcdir/$subdir {*_a.[CHX}]] {
367 # use the FOO_a.C name as the parallelization key
368 if [runtest_file_p $runtests $src] {
369 set tests [lsort [find [file dirname $src] \
370 [regsub {_a.[CHX]$} [file tail $src] {_[a-z].[CHX]}]]]
371
372 if { ![module-check-requirements $tests] } {
373 set testcase [regsub {_a.[CH]} $src {}]
374 set testcase \
375 [string range $testcase [string length "$srcdir/"] end]
376 unsupported $testcase
377 continue
378 }
379
380 set std_list [module-init $src]
381 foreach std $std_list {
382 set mod_files {}
383 global module_do
384 set module_do {"compile" "P"}
385 set asm_list {}
386 set any_hdrs 0
387 global DEFAULT_REPO
388 file_on_host delete $DEFAULT_REPO
389 foreach test $tests {
390 if { [lindex $module_do 1] != "N" } {
391 global module_cmis
392 set module_cmis {}
393 set nshort [file tail [file dirname $test]]/[file tail $test]
394 verbose "Testing $nshort $std" 1
395 switch [file extension $test] {
396 ".C" {
397 lappend asm_list [file rootname [file tail $test]].s
398 }
399 ".X" {
400 set any_hdrs 1
401 }
402 }
403 dg-test -keep-output $test "$std" $DEFAULT_MODFLAGS
404 if { [lindex $module_do 1] == "N" } {
405 break
406 }
407 set testcase [string range $test [string length "$srcdir/"] end]
408 lappend mod_files [module_cmi_p $testcase $module_cmis]
409 }
410 }
411 set testcase [regsub {_a.[CH]} $src {}]
412 set testcase \
413 [string range $testcase [string length "$srcdir/"] end]
414 module_do_it $module_do $testcase $std $asm_list
415 foreach asm $asm_list {
416 file_on_host delete $asm
417 }
418 if { $any_hdrs } {
419 set mod_files [find $DEFAULT_REPO *.gcm]
420 }
421 cleanup_module_files $mod_files
422 }
423 }
424 }
425
426 # Restore the original unsupported proc, lest it will affect
427 # subsequent test runs, or even fail renaming if we run modules.exp
428 # for multiple targets/multilibs/options.
429 rename unsupported {}
430 rename modules-saved-unsupported unsupported
431
432 dg-finish