Skip to content

Commit b7a5722

Browse files
committed
gdb: improve error reporting from expression parser
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 <[email protected]>
1 parent e89496f commit b7a5722

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

gdb/parse.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ parser_state::parse_error (const char *msg)
252252
if (this->prev_lexptr)
253253
this->lexptr = this->prev_lexptr;
254254

255-
error (_("A %s in expression, near `%s'."), msg, this->lexptr);
255+
if (*this->lexptr == '\0')
256+
error (_("A %s in expression, near the end of `%s'."),
257+
msg, this->start_of_input);
258+
else
259+
error (_("A %s in expression, near `%s'."), msg, this->lexptr);
256260
}
257261

258262

gdb/parser-defs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ struct parser_state : public expr_builder
152152
expression_context_block (context_block),
153153
expression_context_pc (context_pc),
154154
lexptr (input),
155+
start_of_input (input),
155156
block_tracker (tracker),
156157
comma_terminates ((flags & PARSER_COMMA_TERMINATES) != 0),
157158
parse_completion (completion),
@@ -288,6 +289,9 @@ struct parser_state : public expr_builder
288289
Currently used only for error reporting. */
289290
const char *prev_lexptr = nullptr;
290291

292+
/* A pointer to the start of the full input, used for error reporting. */
293+
const char *start_of_input = nullptr;
294+
291295
/* Number of arguments seen so far in innermost function call. */
292296

293297
int arglist_len = 0;

gdb/testsuite/gdb.base/exprs.exp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,11 @@ gdb_test "print null_t_struct && null_t_struct->v_int_member == 0" \
275275
# Regression test for unusual function-call parse that caused a crash.
276276
gdb_test "print v_short++(97)" \
277277
"cast the call to its declared return type"
278+
279+
# Test for a syntax error at the end of an expression.
280+
gdb_test "print v_short + " \
281+
"A syntax error in expression, near the end of `v_short \\+'\\."
282+
283+
# Test for a syntax error in the middle of an expression.
284+
gdb_test "print v_short =}{= 3" \
285+
"A syntax error in expression, near `\\}\\{= 3'\\."

0 commit comments

Comments
 (0)