Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [pushed 1/3] [gdb/testsuite] Use transparent_uplevel in foreach_with_prefix
@ 2026-09-24 14:06 Tom de Vries
  2026-09-24 14:06 ` [pushed 2/3] [gdb/testsuite] Add gdb.testsuite/foreach_with_prefix-2.exp Tom de Vries
  2026-09-24 14:06 ` [pushed 3/3] [gdb/testsuite] Simplify foreach_mi_ui_mode Tom de Vries
  0 siblings, 2 replies; 3+ messages in thread
From: Tom de Vries @ 2026-09-24 14:06 UTC (permalink / raw)
  To: gdb-patches

After the 2 previous commits:
- foreach_with_prefix is simplified (foreach handles break and continue), and
- PR34553 is fixed in foreach_with_prefix (added -level increment).

It now looks like this:
...
proc foreach_with_prefix {var list body} {
    upvar 1 $var myvar
    catch {
        foreach myvar $list {
            with_test_prefix "$var=$myvar" {
                uplevel 1 $body
            }
        }
    } result opts
    return -options [dict incr opts -level] $result
...

However, IWBN to use transparent_uplevel, which ideally should replace the
catch, the uplevel and the -level increment.

I finally came up with this solution:
...
    transparent_uplevel [subst_vars {
	foreach $var {$list} {
	    with_test_prefix "$var=[set $var]" {
		$body
 	    }
 	}
    }]
...
which also gets rid of the myvar.

The only gotcha is that the implementation is wrapped in [subst_vars {}],
which provides a way to get values of variables defined in:
- the foreach_with_prefix using $var, and
- the caller using [set var].
---
 gdb/testsuite/lib/gdb.exp | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index cad1ac65913..589e25a12e1 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -3356,15 +3356,13 @@ proc with_test_prefix { prefix body } {
 # including the iterator's name and current value in the prefix.
 
 proc foreach_with_prefix {var list body} {
-    upvar 1 $var myvar
-    catch {
-	foreach myvar $list {
-	    with_test_prefix "$var=$myvar" {
-		uplevel 1 $body
+    transparent_uplevel [subst_vars {
+	foreach $var {$list} {
+	    with_test_prefix "$var=[set $var]" {
+		$body
 	    }
 	}
-    } result opts
-    return -options [dict incr opts -level] $result
+    }]
 }
 
 # Like TCL's native proc, but defines a procedure that wraps its body

base-commit: 0b17fe8b941e1b95535caa61664e96b19c607777
-- 
2.51.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-09-24 14:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-24 14:06 [pushed 1/3] [gdb/testsuite] Use transparent_uplevel in foreach_with_prefix Tom de Vries
2026-09-24 14:06 ` [pushed 2/3] [gdb/testsuite] Add gdb.testsuite/foreach_with_prefix-2.exp Tom de Vries
2026-09-24 14:06 ` [pushed 3/3] [gdb/testsuite] Simplify foreach_mi_ui_mode Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox