From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id 61CFA3858429; Fri, 14 Oct 2022 11:09:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 61CFA3858429 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1665745800; bh=3jgFNzneZuvrjjANLFiI7r9rUu4OD4hMZfj+9trBep8=; h=From:To:Subject:Date:From; b=aZSUWUVRm4eMfyO0eEOwUt/oIlWQjywdGs0JjTPdppoIDOmxogTa3069T9pSStya8 0nMZOpHcnr1OFkyAid3Uck7ztPg5XAjtk3HO0hUBDvEVkAe096Wc+emalLmwWrBRFW 0Zb2kXkZDpFPY5kOKAzqV0FOTJCr4TRigoAUwn1Q= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb/testsuite] Add cond_wrap proc X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: 2ff50ff02d99b27b6b2069c931f759d86afdbbe2 X-Git-Newrev: 1e4be05b752cc94019ed12d6d305e13e55530724 Message-Id: <20221014111000.61CFA3858429@sourceware.org> Date: Fri, 14 Oct 2022 11:09:59 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D1e4be05b752c= c94019ed12d6d305e13e55530724 commit 1e4be05b752cc94019ed12d6d305e13e55530724 Author: Tom de Vries Date: Fri Oct 14 13:09:50 2022 +0200 [gdb/testsuite] Add cond_wrap proc =20 Add a new proc cond_wrap, that can be used to replace the repetitive: ... if { $cond } { wrap { } } else { } ... with the shorter: ... cond_wrap $cond wrap { } ... =20 Tested on x86_64-linux. Diff: --- gdb/testsuite/gdb.testsuite/cond-wrap.exp | 43 +++++++++++++++++++++++++++= ++++ gdb/testsuite/lib/gdb.exp | 20 ++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/gdb/testsuite/gdb.testsuite/cond-wrap.exp b/gdb/testsuite/gdb.= testsuite/cond-wrap.exp new file mode 100644 index 00000000000..7feb88bd102 --- /dev/null +++ b/gdb/testsuite/gdb.testsuite/cond-wrap.exp @@ -0,0 +1,43 @@ +# Copyright 2022 Free Software Foundation, Inc. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# The purpose of this test-case is to test the cond_wrap proc. + +# Proc that temporarily sets global variable a to 6, and executes BODY. +# Note that normally a wrapper needs to guarantee that the restore is exec= uted +# even if executing body throws an error, but we don't need to bother with= this +# for this test-case. +proc wrap { body } { + global a + + # Save a. + set save_a $a + + set a 6 + uplevel 1 $body + + # Restore a. + set a $save_a +} + +# Set initial value of global variable a. +set a 5 + +# Verify values of global variable a for cond =3D=3D 0 and cond =3D=3D 1. +foreach_with_prefix cond {0 1} { + cond_wrap $cond wrap { + gdb_assert {[expr $a - $cond] =3D=3D 5} "value in conditional wrap" + } + gdb_assert {$a =3D=3D 5} "value after conditional wrap" +} diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 61bc060b2f7..3049a7392a4 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -25,6 +25,26 @@ if {$tool =3D=3D ""} { exit 2 } =20 +# Execute BODY, if COND wrapped in proc WRAP. +# Instead of writing the verbose and repetitive: +# if { $cond } { +# wrap $body +# } else { +# $body +# } +# we can use instead: +# cond_wrap $cond wrap $body + +proc cond_wrap { cond wrap body } { + if { $cond } { + $wrap { + uplevel 1 $body + } + } else { + uplevel 1 $body + } +} + # Add VAR_ID=3DVAL to ENV_VAR, unless ENV_VAR already contains a VAR_ID se= tting. =20 proc set_sanitizer_default { env_var var_id val } {