]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: improve error reporting from expression parser
authorAndrew Burgess <aburgess@redhat.com>
Fri, 22 Dec 2023 11:29:13 +0000 (11:29 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Thu, 4 Jan 2024 09:24:18 +0000 (09:24 +0000)
This commits changes how errors are reported from the expression
parser.  Previously, parser errors were reported like this:

  (gdb) p a1 +}= 432
  A syntax error in expression, near `}= 432'.
  (gdb) p a1 +
  A syntax error in expression, near `'.

The first case is fine, a user can figure out what's going wrong, but
the second case is a little confusing; as the error occurred at the
end of the expression GDB just reports the empty string to the user.

After this commit the first case is unchanged, but the second case now
reports like this:

  (gdb) p a1 +
  A syntax error in expression, near the end of `a1 +'.

Which I think is clearer.  There is a possible issue if the expression
being parsed is very long, GDB will repeat the whole expression.  But
this issue already exists in the standard case; if the error occurs
early in a long expression GDB will repeat everything after the syntax
error.  So I've not worried about this case in my new code either,
which keeps things simpler.

I did consider trying to have multi-line errors here, in the style
that gcc produces, with some kind of '~~~~~^' marker on the second
line to indicate where the error occurred; but I rejected this due to
the places in GDB where we catch an error and repackage the message
within some longer string, I don't think multi-line error messages
would work well in that case.  At a minimum it would require some
significant work in order to make all our error handling multi-line
aware.

I've added a couple of extra tests in gdb.base/exprs.exp.

Approved-By: John Baldwin <jhb@FreeBSD.org>
gdb/parse.c
gdb/parser-defs.h
gdb/testsuite/gdb.base/exprs.exp

index efac0dee1af5a4c9d715c4a3ba9fbc36ae7a9764..e8bb112177fc9d0eb46f7525719585905398520e 100644 (file)
@@ -252,7 +252,11 @@ parser_state::parse_error (const char *msg)
   if (this->prev_lexptr)
     this->lexptr = this->prev_lexptr;
 
-  error (_("A %s in expression, near `%s'."), msg, this->lexptr);
+  if (*this->lexptr == '\0')
+    error (_("A %s in expression, near the end of `%s'."),
+          msg, this->start_of_input);
+  else
+    error (_("A %s in expression, near `%s'."), msg, this->lexptr);
 }
 
 \f
index 24522bbbb15fcdbd73a8c0b047c2d71a88dd40d5..44728980a5b897980ced3035f28791b04f032c05 100644 (file)
@@ -152,6 +152,7 @@ struct parser_state : public expr_builder
       expression_context_block (context_block),
       expression_context_pc (context_pc),
       lexptr (input),
+      start_of_input (input),
       block_tracker (tracker),
       comma_terminates ((flags & PARSER_COMMA_TERMINATES) != 0),
       parse_completion (completion),
@@ -288,6 +289,9 @@ struct parser_state : public expr_builder
      Currently used only for error reporting.  */
   const char *prev_lexptr = nullptr;
 
+  /* A pointer to the start of the full input, used for error reporting.  */
+  const char *start_of_input = nullptr;
+
   /* Number of arguments seen so far in innermost function call.  */
 
   int arglist_len = 0;
index 79ae905fccfe6d520b2d2f76c66bd5ec8044f27f..8c85b579b9d6a1c19ddd2446a80b866df03101d0 100644 (file)
@@ -275,3 +275,11 @@ gdb_test "print null_t_struct && null_t_struct->v_int_member == 0" \
 # Regression test for unusual function-call parse that caused a crash.
 gdb_test "print v_short++(97)" \
     "cast the call to its declared return type"
+
+# Test for a syntax error at the end of an expression.
+gdb_test "print v_short + " \
+    "A syntax error in expression, near the end of `v_short \\+'\\."
+
+# Test for a syntax error in the middle of an expression.
+gdb_test "print v_short =}{= 3" \
+    "A syntax error in expression, near `\\}\\{= 3'\\."