[gdb/testsuite] Add cond_wrap proc

Add a new proc cond_wrap, that can be used to replace the repetitive:
...
    if { $cond } {
	wrap {
	    <body>
	}
    } else {
	<body>
    }
...
with the shorter:
...
    cond_wrap $cond wrap {
	<body>
    }
...

Tested on x86_64-linux.
This commit is contained in:
Tom de Vries
2022-10-14 13:09:50 +02:00
parent 2ff50ff02d
commit 1e4be05b75
2 changed files with 63 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
# 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 executed
# 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 == 0 and cond == 1.
foreach_with_prefix cond {0 1} {
cond_wrap $cond wrap {
gdb_assert {[expr $a - $cond] == 5} "value in conditional wrap"
}
gdb_assert {$a == 5} "value after conditional wrap"
}

View File

@ -25,6 +25,26 @@ if {$tool == ""} {
exit 2
}
# 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=VAL to ENV_VAR, unless ENV_VAR already contains a VAR_ID setting.
proc set_sanitizer_default { env_var var_id val } {