1 # Copyright 2015-2022 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 this program. If not, see <http://www.gnu.org/licenses/>.
16 # Generic/oft used support routines for testing GDB's compile feature.
18 # Helper function for skip_compile_feature_tests. This does the real
19 # work, but should not be called directly. Returns a failure reason
20 # (a string) on failure, or the empty string on success.
22 proc _do_check_compile {expr} {
26 gdb_test_multiple "compile code -- $expr;" "check for working compile command" {
27 "Could not load libcc1.*\r\n$gdb_prompt $" {
28 set result "could not find libcc1"
30 "Could not load libcp1.*\r\n$gdb_prompt $" {
31 set result "could not find libcp1"
33 -re "WARNING .* there are active plugins, do not report this" {
34 # Note that the regexp above does not check for the
35 # prompt. This avoids a gratuitous timeout.
36 set result "GCC crashed"
38 -re "confused by earlier errors, bailing out" {
39 # This scenario can happen when either GCC or GDB is
40 # confused by some other debuginfo.
41 # See PR compile/29541.
42 set result "confused by glibc debuginfo"
44 -re "\r\n$gdb_prompt $" {
50 # Return 1 if we should skip tests of the "compile" feature.
51 # This must be invoked after the inferior has been started.
52 # EXPR is the expression to test, if any (using the default empty EXPR
53 # works fine in most cases).
55 proc skip_compile_feature_tests {{expr ""}} {
56 return [expr {[string length [_do_check_compile $expr]] > 0}]
59 # Like skip_compile_feature_tests, but also issue an "untested" when
62 proc skip_compile_feature_untested {{expr ""}} {
63 set output [_do_check_compile $expr]
64 if {[string length $output] > 0} {
65 untested "compile command not supported ($output)"
71 # This namespace provides some convenience functions for running
72 # "compile code" and "compile print" tests.
74 # Exported functions are defined inline below.
78 # Start a new session, noting that the variable "var" will be used for
79 # "compile code" expressions. This variable /must/ exist in the stopped
82 # CompileExpression::new "var"
84 # Test the implicit expression "foo;" with result/value 3.
85 # CompileExpression::test "foo" 3
86 # ---> Runs the following tests (name of tests ignored for illustration)
87 # gdb_test_no_output "compile code var = foo"
88 # gdb_test "p var" "= 3"
89 # gdb_test "compile print foo;" "= 3"
91 # Test the explicit expression "a = function (3); var = a;" with the result 21.
92 # CompileExpression::test "a = function (3); var = a;" 21 -explicit
93 # ---> Runs the following tests (name of tests ignored for illustration)
94 # gdb_test_no_output "compile code a = function (3); var = a;"
95 # gdb_test "p var" "= 21"
97 # Additional option flags may be passed to test to control the behavior
98 # of the test harness:
100 # Pass -explicit to specify that the test uses an explicit expression,
101 # one which sets the value of the variable (see above). Only the code test
104 # Pass -value and/or -print to indicate that the value and/or print steps
105 # will optionally fail. Specify "xfail" or "kfail" to indicate how
106 # particular step will fail. These may be followed by any accepted DejaGNU
107 # parameters such as architecture and bug#. [See examples below.]
109 # To specify that the compile (and consequently print and value tests) is
110 # expected to kfail/xfail, use -kfail or -xfail with any appropriate
111 # DejaGNU parameters. Both options override -print and -value.
112 # [-xfail is given precedence over -kfail should both be given.]
114 # -value is used when a "code" test is run, specifying that the "compile
115 # code" and "print VAR" steps will fail in the prescribed manner.
116 # [If the print step generates a PASS, the test is considered invalidly
117 # written. VAR's value should /always/ be invalidated before a test is
120 # -print is used to specify that an expression will fail in the prescribed
121 # manner when "print" test is executed.
123 # Pass "-name NAME" to set an optional test name. If not specified,
124 # the harness will use test names such as "compile code EXPR" and
125 # "result of compile code EXPR".
127 # Pass "-noprint" or "-nocode" to suppress print or code tests, respectively,
128 # This is useful when the expression being tested modifies the object
129 # being tested, e.g., "a++".
131 # These options must be passed LAST to CompileExpression::test.
135 # Both "code" and "print" tests are expected to xfail:
136 # CompileExpression add_imp "foo" 3 -compile {xfail *-*-*} -print {xfail *-*-*}
138 # The "print $VARIABLE" portion of the "code" test is expected to kfail
139 # (the actual "compile code" GDB command will succeed), but the "print"
141 # CompileExpression add_imp "foo" 3 -value {kfail *-*-* gdb/1234}
143 namespace eval ::CompileExpression {
145 # The variable name to check testing results. This variable
146 # must be in scope when tests are run.
149 # Start a new expression list. VARNAME is the name of the variable
150 # that will be printed to check if the result of the test was
155 set varName_ $varname
158 # Test an expression.
160 # See the preamble for a list of valid optional arguments.
162 # Implicit expressions will be sent to GDB in the form
163 # "$varName = $EXP". "p $varName" will be used to decide the pass
164 # or fail status of the test.
166 # Explicit expressions will be sent to GDB as-is and tested using only
167 # "compile code". The expression should set the value of the variable
168 # $varName, which is then printed to determine whether the test passed
171 # Unlike explicit expressions, implicit expressions are tested with both
172 # "compile print" and "compile code".
174 proc test {exp result args} {
175 parse_args {{value {"" ""}} {print {"" ""}} {name ""}
176 {noprint} {nocode} {explicit} {xfail {"" ""}} {kfail {"" ""}}}
178 if {[lindex $xfail 0] != ""} {
180 } elseif {[lindex $kfail 0] != ""} {
193 do_test_ code $exp $result $explicit $name \
194 [list $compile $value $print]
197 do_test_ print $exp $result $explicit $name \
198 [list $compile $value $print]
202 # Run a compile test for CMD ("print" or "code").
204 proc do_test_ {cmd exp result is_explicit tst fail_list} {
207 if {![string match $cmd "code"]
208 && ![string match $cmd "print"]} {
209 error "invalid command, $cmd; should be \"print\" or \"compile\""
212 # Get expected result of test. Will be "" if test is
214 lassign $fail_list fail_compile fail_value fail_print
216 # Set a test name if one hasn't been provided.
218 set tst "compile $cmd $exp"
221 if {[string match $cmd "print"]} {
223 eval setup_failures_ $fail_print
224 gdb_test "compile print $exp" $result $tst
228 set command "compile code $exp"
230 set command "compile code $varName_ = $exp"
232 eval setup_failures_ $fail_compile
233 gdb_test_no_output $command $tst
234 eval setup_failures_ $fail_value
235 gdb_test "p $varName_" "= $result" "result of $tst"
239 # A convenience proc used to set up xfail and kfail tests.
240 # HOW is either xfail or kfail (case is ignored). ARGS is any
241 # optional architecture, bug number, or other string to pass to
242 # respective DejaGNU setup_$how routines.
244 proc setup_failures_ {how args} {
245 switch -nocase $how {
247 eval setup_xfail $args
251 eval setup_kfail $args
255 # Do nothing. Either the test is expected to PASS
256 # or we have an unhandled failure mode.