[pushed,1/4,gdb/testsuite] Don't use string cat in gdb.dwarf2/dw2-abs-hi-pc.exp

Message ID 20230424124846.29580-1-tdevries@suse.de
State Committed
Headers
Series [pushed,1/4,gdb/testsuite] Don't use string cat in gdb.dwarf2/dw2-abs-hi-pc.exp |

Commit Message

Tom de Vries April 24, 2023, 12:48 p.m. UTC
  Test-case gdb.dwarf2/dw2-abs-hi-pc.exp uses string cat:
...
set sources [lmap i $sources { string cat "${srcdir}/${subdir}/" $i }]
...
but that's only supported starting tcl 8.6.

Fix this by using "expr" instead:
...
set sources [lmap i $sources { expr { "$srcdir/$subdir/$i" } }]
...

Tested on x86_64-linux.
---
 gdb/testsuite/gdb.dwarf2/dw2-abs-hi-pc.exp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)


base-commit: ea5c591c023544e40bb4967314a47d8e6a1e806d
  

Comments

Tom Tromey April 24, 2023, 4:48 p.m. UTC | #1
>>>>> "Tom" == Tom de Vries via Gdb-patches <gdb-patches@sourceware.org> writes:

Tom> Fix this by using "expr" instead:
Tom> ...
Tom> set sources [lmap i $sources { expr { "$srcdir/$subdir/$i" } }]

expr seems very weird here, since it's normally used for evaluating an
expression.

Maybe subst would be a more idiomatic choice, if it works.

Tom
  
Tom de Vries April 24, 2023, 8:22 p.m. UTC | #2
On 4/24/23 18:48, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Tom> Fix this by using "expr" instead:
> Tom> ...
> Tom> set sources [lmap i $sources { expr { "$srcdir/$subdir/$i" } }]
> 
> expr seems very weird here, since it's normally used for evaluating an
> expression.

I got the idea from here ( 
https://stackoverflow.com/questions/30489106/using-lmap-to-filter-list-of-strings 
).

> Maybe subst would be a more idiomatic choice, if it works.

It does, indeed:
...
-set sources [lmap i $sources { expr { "$srcdir/$subdir/$i" } }]
+set sources [lmap i $sources { subst $srcdir/$subdir/$i }]
...

My working set of tcl is a bit random, so I don't know what idiomatic is 
and what not.

I agree that subst is neater since it needs less quoting.

Thanks,
- Tom
  
Tom Tromey April 25, 2023, 6:38 p.m. UTC | #3
>>>>> "Tom" == Tom de Vries via Gdb-patches <gdb-patches@sourceware.org> writes:

Tom> +set sources [lmap i $sources { subst $srcdir/$subdir/$i }]

Tom> My working set of tcl is a bit random, so I don't know what idiomatic
Tom> is and what not.

No worries, Tcl expertise isn't exactly a headline skill.

Tom> I agree that subst is neater since it needs less quoting.

It probably has to be { subst { $srcdir/$subdir/$i } }
since otherwise there will be a second substitution if, say, the srcdir
has a $ or [] in it.

Tom
  
Tom de Vries April 26, 2023, 6:34 a.m. UTC | #4
On 4/25/23 20:38, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Tom> +set sources [lmap i $sources { subst $srcdir/$subdir/$i }]
> 
> Tom> My working set of tcl is a bit random, so I don't know what idiomatic
> Tom> is and what not.
> 
> No worries, Tcl expertise isn't exactly a headline skill.
> 
> Tom> I agree that subst is neater since it needs less quoting.
> 
> It probably has to be { subst { $srcdir/$subdir/$i } }
> since otherwise there will be a second substitution if, say, the srcdir
> has a $ or [] in it.

OK, let's try an example.

We currently have this style:
...
set b d
set l { a $b }
set res [lmap v $l { expr {"$v/c"} }]
foreach v $res {
     puts "V: $v"
}
...
which does:
...
V: a/c
V: $b/c
...

Then with subst we have the same:
...
set res [lmap v $l { subst {$v/c} }]
...
unless we forget the quoting:
...
set res [lmap v $l { subst $v/c }]
...
which gets us instead:
...
V: a/c
V: d/c
...

Hmm, in that case I think subst is the worse choice.  With expr, things 
either parse or not, and if it parses you get the right result.

FWIW, reading a bit more about it I get the impression also set is 
idiomatic, so I came up with this:
...
set res [lmap v $l { set v $v/c ; set v }]
...
which works as well.

Thanks,
- Tom
  
Andreas Schwab April 26, 2023, 7:08 a.m. UTC | #5
On Apr 26 2023, Tom de Vries via Gdb-patches wrote:

> FWIW, reading a bit more about it I get the impression also set is
> idiomatic, so I came up with this:
> ...
> set res [lmap v $l { set v $v/c ; set v }]
> ...
> which works as well.

You won't need the second set, though, since the first set already
returns the assigned value.
  
Tom Tromey April 27, 2023, 1:39 p.m. UTC | #6
>>>>> "Tom" == Tom de Vries via Gdb-patches <gdb-patches@sourceware.org> writes:

Tom> Then with subst we have the same:
Tom> ...
Tom> set res [lmap v $l { subst {$v/c} }]
Tom> ...
Tom> unless we forget the quoting:

This is true for everything in Tcl though.
The quoting matters a lot.

Tom> Hmm, in that case I think subst is the worse choice.  With expr,
Tom> things either parse or not, and if it parses you get the right result.

expr has corner cases where weird things will happen as well.
It's really just intended for use with the expression sub-language.

Tom
  
Tom de Vries May 2, 2023, 3:38 p.m. UTC | #7
On 4/26/23 09:08, Andreas Schwab wrote:
> On Apr 26 2023, Tom de Vries via Gdb-patches wrote:
> 
>> FWIW, reading a bit more about it I get the impression also set is
>> idiomatic, so I came up with this:
>> ...
>> set res [lmap v $l { set v $v/c ; set v }]
>> ...
>> which works as well.
> 
> You won't need the second set, though, since the first set already
> returns the assigned value.
> 

Andreas, thanks for the review.

Committed as attached.

Thanks,
- Tom
  
Tom de Vries May 2, 2023, 3:40 p.m. UTC | #8
On 4/27/23 15:39, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Tom> Then with subst we have the same:
> Tom> ...
> Tom> set res [lmap v $l { subst {$v/c} }]
> Tom> ...
> Tom> unless we forget the quoting:
> 
> This is true for everything in Tcl though.
> The quoting matters a lot.
> 

True.

> Tom> Hmm, in that case I think subst is the worse choice.  With expr,
> Tom> things either parse or not, and if it parses you get the right result.
> 
> expr has corner cases where weird things will happen as well.
> It's really just intended for use with the expression sub-language.

Ack.

I've just pushed a patch that uses set instead of expr, so this is no 
longer an issue.

Thanks for reporting this.

- Tom
  

Patch

diff --git a/gdb/testsuite/gdb.dwarf2/dw2-abs-hi-pc.exp b/gdb/testsuite/gdb.dwarf2/dw2-abs-hi-pc.exp
index 0868b69f15e..2ea6a5cea00 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-abs-hi-pc.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-abs-hi-pc.exp
@@ -24,7 +24,8 @@  set sources \
 	 ${testfile}.c \
 	 ${testfile}-hello.c \
 	 ${testfile}-world.c]
-set sources [lmap i $sources { string cat "${srcdir}/${subdir}/" $i }]
+set sources [lmap i $sources { expr { "$srcdir/$subdir/$i" } }]
+
 lassign [function_range hello $sources] \
     hello_start hello_len
 lassign [function_range world $sources] \