Allow re-assigning to convenience variables
authorTom Tromey <tromey@adacore.com>
Tue, 5 Nov 2019 17:12:27 +0000 (10:12 -0700)
committerTom Tromey <tromey@adacore.com>
Thu, 14 Nov 2019 18:58:50 +0000 (11:58 -0700)
A customer reported somewhat odd gdb behavior, where re-assigning an
array or string to a convenience variable would yield "Too many array
elements".  A test case is:

    (gdb) p $x = "x"
    (gdb) p $x = "xyz"

This patch fixes the problem by making a special case in the evaluator
for assignment to convenience variables, which seems like the correct
behavior.

Note that a previous patch implemented this for Ada, see commit
f411722cb ("Allow re-assigning to convenience variables").

gdb/ChangeLog
2019-11-14  Tom Tromey  <tromey@adacore.com>

* eval.c (evaluate_subexp_standard) <BINOP_ASSIGN>: Do not pass an
expected type for the RHS if the LHS is a convenience variable.

gdb/testsuite/ChangeLog
2019-11-14  Tom Tromey  <tromey@adacore.com>

* gdb.base/gdbvars.exp (test_convenience_variables): Add
regression tests.

Change-Id: I5e66a2d243931a5c43c7af4bc9f6717464c2477e

gdb/ChangeLog
gdb/eval.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/gdbvars.exp

index 719574f30967a35fa71cf16aaddf29dc36d74580..1a84242839b36ecb089a3c878fee429bc3ef1714 100644 (file)
@@ -1,3 +1,8 @@
+2019-11-14  Tom Tromey  <tromey@adacore.com>
+
+       * eval.c (evaluate_subexp_standard) <BINOP_ASSIGN>: Do not pass an
+       expected type for the RHS if the LHS is a convenience variable.
+
 2019-11-14  Simon Marchi  <simon.marchi@polymtl.ca>
 
        * unittests/vec-utils-selftests.c (unordered_remove_tests::obj):
index 139fd68aaa9bcfbca24a8ab56e1c26fb8da4a7ce..71e79d623a3c67d8de7153546f98980e9b1fa03c 100644 (file)
@@ -2151,7 +2151,14 @@ evaluate_subexp_standard (struct type *expect_type,
 
     case BINOP_ASSIGN:
       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
-      arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
+      /* Special-case assignments where the left-hand-side is a
+        convenience variable -- in these, don't bother setting an
+        expected type.  This avoids a weird case where re-assigning a
+        string or array to an internal variable could error with "Too
+        many array elements".  */
+      arg2 = evaluate_subexp (VALUE_LVAL (arg1) == lval_internalvar
+                             ? NULL_TYPE : value_type (arg1),
+                             exp, pos, noside);
 
       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
        return arg1;
index 7416b820d78eab40893b7351ecd96bb0f8bcec89..3a4d2294706afa37026a99fdbffa84a9dadc6e1c 100644 (file)
@@ -1,3 +1,8 @@
+2019-11-14  Tom Tromey  <tromey@adacore.com>
+
+       * gdb.base/gdbvars.exp (test_convenience_variables): Add
+       regression tests.
+
 2019-11-12  Tom Tromey  <tom@tromey.com>
 
        * lib/tuiterm.exp (_accept): Add wait_for parameter.  Check output
index 8259115b7fb2780d157821b386863932b7a6ab45..a4e5b417ac50f394c08d984884829f92b4654e03 100644 (file)
@@ -51,6 +51,21 @@ proc test_convenience_variables {} {
 
     gdb_test "print \$bar"             " = void" \
        "Print contents of uninitialized convenience variable"
+
+
+    gdb_test "print \$str = \"some string\"" \
+       " = \"some string\"" \
+       "Set convenience variable to string value"
+    gdb_test "print \$str = \"some other string\"" \
+       " = \"some other string\"" \
+       "Change convenience variable to different string value"
+
+    gdb_test "print \$arr = {1, 2, 3}" \
+       " = \\{1, 2, 3\\}" \
+       "Set convenience variable to array value"
+    gdb_test "print \$arr = {0, 1, 2, 3}" \
+       " = \\{0, 1, 2, 3\\}" \
+       "Set convenience variable to different array value"
 }
 
 proc test_convenience_functions {} {
This page took 0.03709 seconds and 4 git commands to generate.