[03/11,gdb/testsuite] Simplify foreach_with_prefix

Message ID 20260824135855.1195963-4-tdevries@suse.de
State New
Headers
Series Refactor exception handling |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Tom de Vries Aug. 24, 2026, 1:58 p.m. UTC
  In the testsuite there's a pattern for handling a catch result:
...
proc foo {body} {
  set code [catch {uplevel 1 $body} result]
  if {$code == 1} {
      global errorInfo errorCode
      return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
  } else {
      return -code $code $result
  }
}
...
with the intended effect that executing "foo body" has the same effect as
"body".

The pattern's different though in proc foreach_with_prefix:
...
proc foreach_with_prefix {var list body} {
    upvar 1 $var myvar
    foreach myvar $list {
        with_test_prefix "$var=$myvar" {
            set code [catch {uplevel 1 $body} result]
        }

        if {$code == 1} {
            global errorInfo errorCode
            return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
        } elseif {$code == 3} {
            break
	} elseif {$code == 2} {
            return -code $code $result
        }
    }
}
...
where it handles:
- break ($code == 3) by breaking, and
- ok and continue ($code == 0/4) by ignoring it.

Move the foreach into the catch:
...
    set code [catch {
        foreach myvar $list {
            with_test_prefix "$var=$myvar" {
                uplevel 1 $body
            }
        }
    } result]
...
to let foreach handle break and continue, allowing us to handle the catch
result in the usual way.

Likewise in foreach_mi_ui_mode.
---
 gdb/testsuite/lib/gdb.exp        | 22 +++++++++++-----------
 gdb/testsuite/lib/mi-support.exp | 26 +++++++++++++-------------
 2 files changed, 24 insertions(+), 24 deletions(-)
  

Patch

diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 71e38e4801a..c8254ac9dd3 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -3305,19 +3305,19 @@  proc with_test_prefix { prefix body } {
 
 proc foreach_with_prefix {var list body} {
     upvar 1 $var myvar
-    foreach myvar $list {
-	with_test_prefix "$var=$myvar" {
-	    set code [catch {uplevel 1 $body} result]
+    set code [catch {
+	foreach myvar $list {
+	    with_test_prefix "$var=$myvar" {
+		uplevel 1 $body
+	    }
 	}
+    } result]
 
-	if {$code == 1} {
-	    global errorInfo errorCode
-	    return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
-	} elseif {$code == 3} {
-	    break
-	} elseif {$code == 2} {
-	    return -code $code $result
-	}
+    if {$code == 1} {
+	global errorInfo errorCode
+	return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
+    } else {
+	return -code $code $result
     }
 }
 
diff --git a/gdb/testsuite/lib/mi-support.exp b/gdb/testsuite/lib/mi-support.exp
index 4fd3eeb0dde..2e87769b538 100644
--- a/gdb/testsuite/lib/mi-support.exp
+++ b/gdb/testsuite/lib/mi-support.exp
@@ -2931,19 +2931,19 @@  proc foreach_mi_ui_mode { var_name body } {
        set modes {"main" "separate"}
     }
 
-    foreach var $modes {
-       with_test_prefix "$var_name=$var" {
-	   set code [catch {uplevel 1 $body} result]
-       }
-
-       if {$code == 1} {
-	   global errorInfo errorCode
-	   return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
-       } elseif {$code == 3} {
-	   break
-       } elseif {$code == 2} {
-	   return -code $code $result
-       }
+    set code [catch {
+	foreach var $modes {
+	    with_test_prefix "$var_name=$var" {
+		uplevel 1 $body
+	    }
+	}
+    } result]
+
+    if {$code == 1} {
+	global errorInfo errorCode
+	return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
+    } else {
+	return -code $code $result
     }
 }