xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/lib/compile-support.exp (revision 70f7362772ba52b749c976fb5e86e39a8b2c9afc)
1# Copyright 2015-2020 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 this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# Generic/oft used support routines for testing GDB's compile feature.
17
18# Return 1 if we should skip tests of the "compile" feature.
19# This must be invoked after the inferior has been started.
20
21proc skip_compile_feature_tests {} {
22    global gdb_prompt
23
24    set result 0
25    gdb_test_multiple "compile code -- ;" "check for working compile command" {
26	"Could not load libcc1.*\r\n$gdb_prompt $" {
27	    set result 1
28	}
29	-re "Command not supported on this host\\..*\r\n$gdb_prompt $" {
30	    set result 1
31	}
32	-re "\r\n$gdb_prompt $" {
33	}
34    }
35    return $result
36}
37
38# This namespace provides some convenience functions for running
39# "compile code" and "compile print" tests.
40#
41# Exported functions are defined inline below.
42#
43# General usage:
44#
45# Start a new session, noting that the variable "var" will be used for
46# "compile code" expressions.  This variable /must/ exist in the stopped
47# location.
48#
49# CompileExpression::new "var"
50#
51# Test the implicit expression "foo;" with result/value 3.
52# CompileExpression::test "foo" 3
53# ---> Runs the following tests (name of tests ignored for illustration)
54#    gdb_test_no_output "compile code var = foo"
55#    gdb_test "p var" "= 3"
56#    gdb_test "compile print foo;" "= 3"
57#
58# Test the explicit expression "a = function (3); var = a;" with the result 21.
59# CompileExpression::test "a = function (3); var = a;" 21 -explicit
60# ---> Runs the following tests (name of tests ignored for illustration)
61#    gdb_test_no_output "compile code a = function (3); var = a;"
62#    gdb_test "p var" "= 21"
63#
64# Additional option flags may be passed to test to control the behavior
65# of the test harness:
66#
67# Pass -explicit to specify that the test uses an explicit expression,
68# one which sets the value of the variable (see above).  Only the code test
69# will be run.
70#
71# Pass -value and/or -print to indicate that the value and/or print steps
72# will optionally fail. Specify "xfail" or "kfail" to indicate how
73# particular step will fail.  These may be followed by any accepted DejaGNU
74# parameters such as architecture and bug#.  [See examples below.]
75#
76# To specify that the compile (and consequently print and value tests) is
77# expected to kfail/xfail, use -kfail or -xfail with any appropriate
78# DejaGNU parameters.  Both options override -print and -value.
79# [-xfail is given precedence over -kfail should both be given.]
80#
81# -value is used when a "code" test is run, specifying that the "compile
82# code" and "print VAR" steps will fail in the prescribed manner.
83# [If the print step generates a PASS, the test is considered invalidly
84# written.  VAR's value should /always/ be invalidated before a test is
85# run.]
86#
87# -print is used to specify that an expression will fail in the prescribed
88# manner when "print" test is executed.
89#
90# Pass "-name NAME" to set an optional test name.  If not specified,
91# the harness will use test names such as "compile code EXPR" and
92# "result of compile code EXPR".
93#
94# Pass "-noprint" or "-nocode" to suppress print or code tests, respectively,
95# This is useful when the expression being tested modifies the object
96# being tested, e.g., "a++".
97#
98# These options must be passed LAST to CompileExpression::test.
99#
100# Examples:
101#
102# Both "code" and "print" tests are expected to xfail:
103# CompileExpression add_imp "foo" 3 -compile {xfail *-*-*} -print {xfail *-*-*}
104#
105# The "print $VARIABLE" portion of the "code" test is expected to kfail
106# (the actual "compile code" GDB command will succeed), but the "print"
107# test should pass:
108# CompileExpression add_imp "foo" 3 -value {kfail *-*-* gdb/1234}
109
110namespace eval ::CompileExpression {
111
112    # The variable name to check testing results.  This variable
113    # must be in scope when tests are run.
114    variable varName_ {}
115
116    # Start a new expression list.  VARNAME is the name of the variable
117    # that will be printed to check if the result of the test was
118    # successful.
119    proc new {varname} {
120	variable varName_
121
122	set varName_ $varname
123    }
124
125    # Test an expression.
126    #
127    # See the preamble for a list of valid optional arguments.
128    #
129    # Implicit expressions will be sent to GDB in the form
130    # "$varName = $EXP".  "p $varName" will be used to decide the pass
131    # or fail status of the test.
132    #
133    # Explicit expressions will be sent to GDB as-is and tested using only
134    # "compile code".  The expression should set the value of the variable
135    # $varName, which is then printed to determine whether the test passed
136    # or failed.
137    #
138    # Unlike explicit expressions, implicit expressions are tested with both
139    # "compile print" and "compile code".
140
141    proc test {exp result args} {
142	parse_args {{value {"" ""}} {print {"" ""}} {name ""}
143	    {noprint} {nocode} {explicit} {xfail {"" ""}} {kfail {"" ""}}}
144
145	if {[lindex $xfail 0] != ""} {
146	    set l "xfail $xfail"
147	} elseif {[lindex $kfail 0] != ""} {
148	    set l "kfail $kfail"
149	} else {
150	    set l ""
151	    set compile {"" ""}
152	}
153	if {$l != ""} {
154	    set compile $l
155	    set print $l
156	    set value $l
157	}
158
159	if {!$nocode} {
160	    do_test_ code $exp $result $explicit $name \
161		[list $compile $value $print]
162	}
163	if {!$noprint} {
164	    do_test_ print $exp $result $explicit $name \
165		[list $compile $value $print]
166	}
167    }
168
169    # Run a compile test for CMD ("print" or "code").
170
171    proc do_test_ {cmd exp result is_explicit tst fail_list} {
172	variable varName_
173
174	if {![string match $cmd "code"]
175	    && ![string match $cmd "print"]} {
176	    error "invalid command, $cmd; should be \"print\" or \"compile\""
177	}
178
179	# Get expected result of test.  Will be "" if test is
180	# expected to PASS.
181	lassign $fail_list fail_compile fail_value fail_print
182
183	# Set a test name if one hasn't been provided.
184	if {$tst == ""} {
185	    set tst "compile $cmd $exp"
186	}
187
188	if {[string match $cmd "print"]} {
189	    if {!$is_explicit} {
190		eval setup_failures_ $fail_print
191		gdb_test "compile print $exp" $result $tst
192	    }
193	} else {
194	    if {$is_explicit} {
195		set command "compile code $exp"
196	    } else {
197		set command "compile code $varName_ = $exp"
198	    }
199	    eval setup_failures_ $fail_compile
200	    gdb_test_no_output $command $tst
201	    eval setup_failures_ $fail_value
202	    gdb_test "p $varName_" "= $result" "result of $tst"
203	}
204    }
205
206    # A convenience proc used to set up xfail and kfail tests.
207    # HOW is either xfail or kfail (case is ignored).  ARGS is any
208    # optional architecture, bug number, or other string to pass to
209    # respective DejaGNU setup_$how routines.
210
211    proc setup_failures_ {how args} {
212	switch -nocase $how {
213	    xfail {
214		eval setup_xfail $args
215	    }
216
217	    kfail {
218		eval setup_kfail $args
219	    }
220
221	    default {
222		# Do nothing.  Either the test is expected to PASS
223		# or we have an unhandled failure mode.
224	    }
225	}
226    }
227}
228