1#   Copyright (C) 1997-2013 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# DejaGnu's dg-test defines extra flags that are used to compile a test.
18# Access them for directives that need to examine all options that are
19# used for a test, including checks for non-cached effective targets.
20# We don't know how far up the call chain it is but we know we'll hit
21# it eventually, and that we're at least 3 calls down.
22
23proc current_compiler_flags { } {
24    set frames 2
25    while { ![info exists flags1] } {
26	set frames [expr $frames + 1]
27	upvar $frames dg-extra-tool-flags flags1
28    }
29    upvar $frames tool_flags flags2
30    return "$flags1 $flags2"
31}
32
33# DejaGnu's dg-test defines a test name that includes torture options
34# which is used in most pass/fail messages.  Grab a copy of it.
35
36proc testname-for-summary { } {
37    global testname_with_flags
38
39    # A variable called "name" is too generic, so identify dg-test by
40    # the existence of dg-extra-tool-flags.
41    if ![info exists testname_with_flags] {
42	set frames 2
43	while { ![info exists flags] } {
44	    set frames [expr $frames + 1]
45	    upvar $frames dg-extra-tool-flags flags
46	}
47
48	# We've got the stack level for dg-test; get the variable we want.
49	upvar $frames name name
50	set testname_with_flags $name
51
52	# If there are flags, add an extra space to improve readability of
53	# the test summary.
54	if { [llength $testname_with_flags] > 1 } {
55	    set testname_with_flags "$testname_with_flags "
56	}
57    }
58    return "$testname_with_flags"
59}
60
61# If this target does not support weak symbols, skip this test.
62
63proc dg-require-weak { args } {
64    set weak_available [ check_weak_available ]
65    if { $weak_available == -1 } {
66	upvar name name
67	unresolved "$name"
68    }
69    if { $weak_available != 1 } {
70	upvar dg-do-what dg-do-what
71	set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
72    }
73}
74
75# If this target does not support overriding weak symbols, skip this
76# test.
77
78proc dg-require-weak-override { args } {
79    set weak_override_available [ check_weak_override_available ]
80    if { $weak_override_available == -1 } {
81	upvar name name
82	unresolved "$name"
83    }
84    if { $weak_override_available != 1 } {
85	upvar dg-do-what dg-do-what
86	set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
87    }
88}
89
90# If this target does not support the "visibility" attribute, skip this
91# test.
92
93proc dg-require-visibility { args } {
94    set visibility_available [ check_visibility_available [lindex $args 1 ] ]
95    if { $visibility_available == -1 } {
96	upvar name name
97	unresolved "$name"
98    }
99    if { $visibility_available != 1 } {
100	upvar dg-do-what dg-do-what
101	set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
102    }
103}
104
105# If this target does not support the "alias" attribute, skip this
106# test.
107
108proc dg-require-alias { args } {
109    set alias_available [ check_alias_available ]
110    if { $alias_available == -1 } {
111	upvar name name
112	unresolved "$name"
113    }
114    if { $alias_available < 2 } {
115	upvar dg-do-what dg-do-what
116	set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
117    }
118}
119
120# If this target does not support the "ifunc" attribute, skip this
121# test.
122
123proc dg-require-ifunc { args } {
124    if { ![ check_ifunc_available ] } {
125	upvar dg-do-what dg-do-what
126	set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
127    }
128}
129
130# If this target's linker does not support the --gc-sections flag,
131# skip this test.
132
133proc dg-require-gc-sections { args } {
134    if { ![ check_gc_sections_available ] } {
135	upvar dg-do-what dg-do-what
136	set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
137    }
138}
139
140# If this target does not support profiling, skip this test.
141
142proc dg-require-profiling { args } {
143    if { ![ check_profiling_available [lindex $args 1] ] } {
144	upvar dg-do-what dg-do-what
145	set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
146    }
147}
148
149# If this target does not support DLL attributes skip this test.
150
151proc dg-require-dll { args } {
152    # As a special case, the mcore-*-elf supports these attributes.
153    # All Symbian OS targets also support these attributes.
154    if { [istarget mcore-*-elf]
155         || [istarget *-*-symbianelf] } {
156	return
157    }
158    # PE/COFF targets support dllimport/dllexport.
159    if { [gcc_target_object_format] == "pe" } {
160	return
161    }
162
163    upvar dg-do-what dg-do-what
164    set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
165}
166
167# If this host does not support an ASCII locale, skip this test.
168
169proc dg-require-ascii-locale { args } {
170    if { ![ check_ascii_locale_available] } {
171	upvar dg-do-what dg-do-what
172        set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
173    }
174}
175
176proc dg-require-iconv { args } {
177    if { ![ check_iconv_available ${args} ] } {
178	upvar dg-do-what dg-do-what
179        set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
180    }
181}
182
183# If this target does not support named sections skip this test.
184
185proc dg-require-named-sections { args } {
186    if { ![ check_named_sections_available ] } {
187	upvar dg-do-what dg-do-what
188        set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
189    }
190}
191
192# If the target does not match the required effective target, skip this test.
193# Only apply this if the optional selector matches.
194
195proc dg-require-effective-target { args } {
196    set args [lreplace $args 0 0]
197    # Verify the number of arguments.  The last is optional.
198    if { [llength $args] < 1 || [llength $args] > 2 } {
199	error "syntax error, need a single effective-target keyword with optional selector"
200    }
201
202    # Don't bother if we're already skipping the test.
203    upvar dg-do-what dg-do-what
204    if { [lindex ${dg-do-what} 1] == "N" } {
205      return
206    }
207
208    # Evaluate selector if present.
209    if { [llength $args] == 2 } {
210	switch [dg-process-target-1 [lindex $args 1]] {
211	    "S" { }
212	    "N" { return }
213	}
214    }
215
216    if { ![is-effective-target [lindex $args 0]] } {
217        set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
218    }
219}
220
221# If this target does not have fork, skip this test.
222
223proc dg-require-fork { args } {
224    if { ![check_fork_available] } {
225	upvar dg-do-what dg-do-what
226        set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
227    }
228}
229
230# If this target does not have mkfifo, skip this test.
231
232proc dg-require-mkfifo { args } {
233    if { ![check_mkfifo_available] } {
234	upvar dg-do-what dg-do-what
235        set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
236    }
237}
238
239# If this target does not use __cxa_atexit, skip this test.
240
241proc dg-require-cxa-atexit { args } {
242    if { ![ check_cxa_atexit_available ] } {
243        upvar dg-do-what dg-do-what
244        set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
245    }
246}
247
248# If the host is remote rather than the same as the build system, skip
249# this test.  Some tests are incompatible with DejaGnu's handling of
250# remote hosts, which involves copying the source file to the host and
251# compiling it with a relative path and "-o a.out".
252
253proc dg-require-host-local { args } {
254    if [ is_remote host ] {
255        upvar dg-do-what dg-do-what
256        set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
257    }
258}
259
260proc dg-require-linker-plugin { args } {
261    set linker_plugin_available [ check_linker_plugin_available ]
262    if { $linker_plugin_available == 0 } {
263	upvar dg-do-what dg-do-what
264	set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
265    }
266}
267
268# Add any target-specific flags needed for accessing the given list
269# of features.  This must come after all dg-options.
270
271proc dg-add-options { args } {
272    upvar dg-extra-tool-flags extra-tool-flags
273
274    foreach arg [lrange $args 1 end] {
275	if { [info procs add_options_for_$arg] != "" } {
276	    set extra-tool-flags \
277		[eval [list add_options_for_$arg ${extra-tool-flags}]]
278	} else {
279	    error "Unrecognized option type: $arg"
280	}
281    }
282}
283
284# Compare flags for a test directive against flags that will be used to
285# compile the test: multilib flags, flags for torture options, and either
286# the default flags for this group of tests or flags specified with a
287# previous dg-options directive.
288
289proc check-flags { args } {
290    global compiler_flags
291    global TOOL_OPTIONS
292    global TEST_ALWAYS_FLAGS
293
294    # The args are within another list; pull them out.
295    set args [lindex $args 0]
296
297    # Start the list with a dummy tool name so the list will match "*"
298    # if there are no flags.
299    set compiler_flags " toolname "
300    append compiler_flags [current_compiler_flags]
301    # If running a subset of the test suite, $TOOL_OPTIONS may not exist.
302    catch {append compiler_flags " $TOOL_OPTIONS "}
303    # If running a subset of the test suite, $TEST_ALWAYS_FLAGS may not exist.
304    catch {append compiler_flags " $TEST_ALWAYS_FLAGS "}
305    set dest [target_info name]
306    if [board_info $dest exists cflags] {
307	append compiler_flags "[board_info $dest cflags] "
308    }
309    if [board_info $dest exists multilib_flags] {
310	append compiler_flags "[board_info $dest multilib_flags] "
311    }
312
313    # The next two arguments are optional.  If they were not specified,
314    # use the defaults.
315    if { [llength $args] == 2 } {
316	lappend $args [list "*"]
317    }
318    if { [llength $args] == 3 } {
319	lappend $args [list ""]
320    }
321
322    # If the option strings are the defaults, or the same as the
323    # defaults, there is no need to call check_conditional_xfail to
324    # compare them to the actual options.
325    if { [string compare [lindex $args 2] "*"] == 0
326	 && [string compare [lindex $args 3] "" ] == 0 } {
327	set result 1
328    } else {
329	# The target list might be an effective-target keyword, so replace
330	# the original list with "*-*-*", since we already know it matches.
331	set result [check_conditional_xfail [lreplace $args 1 1 "*-*-*"]]
332    }
333
334    # Any value in this variable was left over from an earlier test.
335    set compiler_flags ""
336
337    return $result
338}
339
340# Skip the test (report it as UNSUPPORTED) if the target list and
341# included flags are matched and the excluded flags are not matched.
342#
343# The first argument is the line number of the dg-skip-if directive
344# within the test file.   Remaining arguments are as for xfail lists:
345# message { targets } { include } { exclude }
346#
347# This tests against multilib flags plus either the default flags for this
348# group of tests or flags specified with a previous dg-options command.
349
350proc dg-skip-if { args } {
351    # Verify the number of arguments.  The last two are optional.
352    set args [lreplace $args 0 0]
353    if { [llength $args] < 2 || [llength $args] > 4 } {
354	error "dg-skip-if 2: need 2, 3, or 4 arguments"
355    }
356
357    # Don't bother if we're already skipping the test.
358    upvar dg-do-what dg-do-what
359    if { [lindex ${dg-do-what} 1] == "N" } {
360      return
361    }
362
363    set selector [list target [lindex $args 1]]
364    if { [dg-process-target-1 $selector] == "S" } {
365	if [check-flags $args] {
366	    upvar dg-do-what dg-do-what
367            set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
368	}
369    }
370}
371
372# Like check_conditional_xfail, but callable from a dg test.
373
374proc dg-xfail-if { args } {
375    # Verify the number of arguments.  The last three are optional.
376    set args [lreplace $args 0 0]
377    if { [llength $args] < 2 || [llength $args] > 4 } {
378	error "dg-xfail-if: need 2, 3, or 4 arguments"
379    }
380
381    # Don't change anything if we're already skipping the test.
382    upvar dg-do-what dg-do-what
383    if { [lindex ${dg-do-what} 1] == "N" } {
384      return
385    }
386
387    set selector [list target [lindex $args 1]]
388    if { [dg-process-target-1 $selector] == "S" } {
389	global compiler_conditional_xfail_data
390
391	# The target list might be an effective-target keyword.  Replace
392	# the original list with "*-*-*", since we already know it matches.
393	set args [lreplace $args 1 1 "*-*-*"]
394
395	# Supply default values for unspecified optional arguments.
396	if { [llength $args] == 2 } {
397	    lappend $args [list "*"]
398	}
399	if { [llength $args] == 3 } {
400	    lappend $args [list ""]
401	}
402
403	set compiler_conditional_xfail_data $args
404    }
405}
406
407# Like dg-xfail-if but for the execute step.
408
409proc dg-xfail-run-if { args } {
410    # Verify the number of arguments.  The last two are optional.
411    set args [lreplace $args 0 0]
412    if { [llength $args] < 2 || [llength $args] > 4 } {
413	error "dg-xfail-run-if: need 2, 3, or 4 arguments"
414    }
415
416    # Don't bother if we're already skipping the test.
417    upvar dg-do-what dg-do-what
418    if { [lindex ${dg-do-what} 1] == "N" } {
419      return
420    }
421
422    set selector [list target [lindex $args 1]]
423    if { [dg-process-target-1 $selector] == "S" } {
424	if [check-flags $args] {
425	    upvar dg-do-what dg-do-what
426            set dg-do-what [list [lindex ${dg-do-what} 0] "S" "F"]
427	}
428    }
429}
430
431# Record whether the program is expected to return a nonzero status.
432
433set shouldfail 0
434
435proc dg-shouldfail { args } {
436    # Don't bother if we're already skipping the test.
437    upvar dg-do-what dg-do-what
438    if { [lindex ${dg-do-what} 1] == "N" } {
439      return
440    }
441
442    global shouldfail
443
444    set args [lreplace $args 0 0]
445    if { [llength $args] > 1 } {
446	set selector [list target [lindex $args 1]]
447	if { [dg-process-target-1 $selector] == "S" } {
448	    # The target matches, now check the flags.
449	    if [check-flags $args] {
450		set shouldfail 1
451	    }
452	}
453    } else {
454	set shouldfail 1
455    }
456}
457
458# Intercept the call to the DejaGnu version of dg-process-target to
459# support use of an effective-target keyword in place of a list of
460# target triplets to xfail or skip a test.
461#
462# The argument to dg-process-target is the keyword "target" or "xfail"
463# followed by a selector:
464#    target-triplet-1 ...
465#    effective-target-keyword
466#    selector-expression
467#
468# For a target list the result is "S" if the target is selected, "N" otherwise.
469# For an xfail list the result is "F" if the target is affected, "P" otherwise.
470
471# In contexts that allow either "target" or "xfail" the argument can be
472#    target selector1 xfail selector2
473# which returns "N" if selector1 is not selected, otherwise the result of
474# "xfail selector2".
475#
476# A selector expression appears within curly braces and uses a single logical
477# operator: !, &&, or ||.  An operand is another selector expression, an
478# effective-target keyword, or a list of target triplets within quotes or
479# curly braces.
480
481if { [info procs saved-dg-process-target] == [list] } {
482    rename dg-process-target saved-dg-process-target
483
484    # Evaluate an operand within a selector expression.
485    proc selector_opd { op } {
486	set selector "target"
487	lappend selector $op
488	set answer [ expr { [dg-process-target $selector] == "S" } ]
489	verbose "selector_opd: `$op' $answer" 2
490	return $answer
491    }
492
493    # Evaluate a target triplet list within a selector expression.
494    # Unlike other operands, this needs to be expanded from a list to
495    # the same string as "target".
496    proc selector_list { op } {
497	set selector "target [join $op]"
498	set answer [ expr { [dg-process-target $selector] == "S" } ]
499	verbose "selector_list: `$op' $answer" 2
500	return $answer
501    }
502
503    # Evaluate a selector expression.
504    proc selector_expression { exp } {
505	if { [llength $exp] == 2 } {
506	    if [string match "!" [lindex $exp 0]] {
507		set op1 [lindex $exp 1]
508		set answer [expr { ! [selector_opd $op1] }]
509	    } else {
510		# Assume it's a list of target triplets.
511		set answer [selector_list $exp]
512	    }
513	} elseif { [llength $exp] == 3 } {
514	    set op1 [lindex $exp 0]
515	    set opr [lindex $exp 1]
516	    set op2 [lindex $exp 2]
517	    if [string match "&&" $opr] {
518		set answer [expr { [selector_opd $op1] && [selector_opd $op2] }]
519	    } elseif [string match "||" $opr] {
520		set answer [expr { [selector_opd $op1] || [selector_opd $op2] }]
521	    } else {
522		# Assume it's a list of target triplets.
523		set answer [selector_list $exp]
524	    }
525	} else {
526	    # Assume it's a list of target triplets.
527	    set answer [selector_list $exp]
528	}
529
530	verbose "selector_expression: `$exp' $answer" 2
531	return $answer
532    }
533
534    # Evaluate "target selector" or "xfail selector".
535
536    proc dg-process-target-1 { args } {
537	verbose "dg-process-target-1: `$args'" 2
538
539	# Extract the 'what' keyword from the argument list.
540	set selector [string trim [lindex $args 0]]
541	if [regexp "^xfail " $selector] {
542	    set what "xfail"
543	} elseif [regexp "^target " $selector] {
544	    set what "target"
545	} else {
546	    error "syntax error in target selector \"$selector\""
547	}
548
549	# Extract the rest of the list, which might be a keyword.
550	regsub "^${what}" $selector "" rest
551	set rest [string trim $rest]
552
553	if [is-effective-target-keyword $rest] {
554	    # The selector is an effective target keyword.
555	    if [is-effective-target $rest] {
556		return [expr { $what == "xfail" ? "F" : "S" }]
557	    } else {
558		return [expr { $what == "xfail" ? "P" : "N" }]
559	    }
560	}
561
562	if [string match "{*}" $rest] {
563	    if [selector_expression [lindex $rest 0]] {
564		return [expr { $what == "xfail" ? "F" : "S" }]
565	    } else {
566		return [expr { $what == "xfail" ? "P" : "N" }]
567	    }
568	}
569
570	# The selector is not an effective-target keyword, so process
571	# the list of target triplets.
572	return [saved-dg-process-target $selector]
573    }
574
575    # Intercept calls to the DejaGnu function.  In addition to
576    # processing "target selector" or "xfail selector", handle
577    # "target selector1 xfail selector2".
578
579    proc dg-process-target { args } {
580	verbose "replacement dg-process-target: `$args'" 2
581
582	set selector [string trim [lindex $args 0]]
583
584	# If the argument list contains both 'target' and 'xfail',
585	# process 'target' and, if that succeeds, process 'xfail'.
586	if [regexp "^target .* xfail .*" $selector] {
587	    set xfail_index [string first "xfail" $selector]
588	    set xfail_selector [string range $selector $xfail_index end]
589	    set target_selector [string range $selector 0 [expr $xfail_index-1]]
590	    set target_selector [string trim $target_selector]
591	    if { [dg-process-target-1 $target_selector] == "N" } {
592		return "N"
593	    }
594	    return [dg-process-target-1 $xfail_selector]
595
596	}
597	return [dg-process-target-1 $selector]
598    }
599}
600