]> Git Repo - binutils.git/blob - gdb/testsuite/lib/compile-support.exp
Automatic date update in version.in
[binutils.git] / gdb / testsuite / lib / compile-support.exp
1 # Copyright 2015-2022 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 # 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.
21
22 proc _do_check_compile {expr} {
23     global gdb_prompt
24
25     set result ""
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"
29         }
30         "Could not load libcp1.*\r\n$gdb_prompt $" {
31             set result "could not find libcp1"
32         }
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"
37         }
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"
43         }
44         -re "\r\n$gdb_prompt $" {
45         }
46     }
47     return $result
48 }
49
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).
54
55 proc skip_compile_feature_tests {{expr ""}} {
56     return [expr {[string length [_do_check_compile $expr]] > 0}]
57 }
58
59 # Like skip_compile_feature_tests, but also issue an "untested" when
60 # skipping.
61
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)"
66         return 1
67     }
68     return 0
69 }
70
71 # This namespace provides some convenience functions for running
72 # "compile code" and "compile print" tests.
73 #
74 # Exported functions are defined inline below.
75 #
76 # General usage:
77 #
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
80 # location.
81 #
82 # CompileExpression::new "var"
83 #
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"
90 #
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"
96 #
97 # Additional option flags may be passed to test to control the behavior
98 # of the test harness:
99 #
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
102 # will be run.
103 #
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.]
108 #
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.]
113 #
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
118 # run.]
119 #
120 # -print is used to specify that an expression will fail in the prescribed
121 # manner when "print" test is executed.
122 #
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".
126 #
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++".
130 #
131 # These options must be passed LAST to CompileExpression::test.
132 #
133 # Examples:
134 #
135 # Both "code" and "print" tests are expected to xfail:
136 # CompileExpression add_imp "foo" 3 -compile {xfail *-*-*} -print {xfail *-*-*}
137 #
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"
140 # test should pass:
141 # CompileExpression add_imp "foo" 3 -value {kfail *-*-* gdb/1234}
142
143 namespace eval ::CompileExpression {
144
145     # The variable name to check testing results.  This variable
146     # must be in scope when tests are run.
147     variable varName_ {}
148
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
151     # successful.
152     proc new {varname} {
153         variable varName_
154
155         set varName_ $varname
156     }
157
158     # Test an expression.
159     #
160     # See the preamble for a list of valid optional arguments.
161     #
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.
165     #
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
169     # or failed.
170     #
171     # Unlike explicit expressions, implicit expressions are tested with both
172     # "compile print" and "compile code".
173
174     proc test {exp result args} {
175         parse_args {{value {"" ""}} {print {"" ""}} {name ""}
176             {noprint} {nocode} {explicit} {xfail {"" ""}} {kfail {"" ""}}}
177
178         if {[lindex $xfail 0] != ""} {
179             set l "xfail $xfail"
180         } elseif {[lindex $kfail 0] != ""} {
181             set l "kfail $kfail"
182         } else {
183             set l ""
184             set compile {"" ""}
185         }
186         if {$l != ""} {
187             set compile $l
188             set print $l
189             set value $l
190         }
191
192         if {!$nocode} {
193             do_test_ code $exp $result $explicit $name \
194                 [list $compile $value $print]
195         }
196         if {!$noprint} {
197             do_test_ print $exp $result $explicit $name \
198                 [list $compile $value $print]
199         }
200     }
201
202     # Run a compile test for CMD ("print" or "code").
203
204     proc do_test_ {cmd exp result is_explicit tst fail_list} {
205         variable varName_
206
207         if {![string match $cmd "code"]
208             && ![string match $cmd "print"]} {
209             error "invalid command, $cmd; should be \"print\" or \"compile\""
210         }
211
212         # Get expected result of test.  Will be "" if test is
213         # expected to PASS.
214         lassign $fail_list fail_compile fail_value fail_print
215
216         # Set a test name if one hasn't been provided.
217         if {$tst == ""} {
218             set tst "compile $cmd $exp"
219         }
220
221         if {[string match $cmd "print"]} {
222             if {!$is_explicit} {
223                 eval setup_failures_ $fail_print
224                 gdb_test "compile print $exp" $result $tst
225             }
226         } else {
227             if {$is_explicit} {
228                 set command "compile code $exp"
229             } else {
230                 set command "compile code $varName_ = $exp"
231             }
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"
236         }
237     }
238
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.
243
244     proc setup_failures_ {how args} {
245         switch -nocase $how {
246             xfail {
247                 eval setup_xfail $args
248             }
249
250             kfail {
251                 eval setup_kfail $args
252             }
253
254             default {
255                 # Do nothing.  Either the test is expected to PASS
256                 # or we have an unhandled failure mode.
257             }
258         }
259     }
260 }
This page took 0.039956 seconds and 4 git commands to generate.