Skip to content

Commit e89496f

Browse files
committed
gdb: merge error handling from different expression parsers
Many (all?) of the expression parsers implement yyerror to handle parser errors, and all of these functions are basically identical. This commit adds a new parser_state::parse_error() function, which implements the common error handling code, this function can then be called from all the different yyerror functions. The benefit of this is that (in a future commit) I can improve the error output, and all the expression parsers will benefit. This commit is pure refactoring though, and so, there should be no user visible changes after this commit. Approved-By: John Baldwin <[email protected]>
1 parent c9f1e0d commit e89496f

File tree

9 files changed

+23
-25
lines changed

9 files changed

+23
-25
lines changed

gdb/ada-exp.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ ada_parse (struct parser_state *par_state)
12121212
static void
12131213
yyerror (const char *msg)
12141214
{
1215-
error (_("Error in expression, near `%s'."), pstate->lexptr);
1215+
pstate->parse_error (msg);
12161216
}
12171217

12181218
/* Emit expression to access an instance of SYM, in block BLOCK (if

gdb/c-exp.y

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3482,8 +3482,5 @@ c_print_token (FILE *file, int type, YYSTYPE value)
34823482
static void
34833483
yyerror (const char *msg)
34843484
{
3485-
if (pstate->prev_lexptr)
3486-
pstate->lexptr = pstate->prev_lexptr;
3487-
3488-
error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
3485+
pstate->parse_error (msg);
34893486
}

gdb/d-exp.y

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,9 +1631,6 @@ d_parse (struct parser_state *par_state)
16311631
static void
16321632
yyerror (const char *msg)
16331633
{
1634-
if (pstate->prev_lexptr)
1635-
pstate->lexptr = pstate->prev_lexptr;
1636-
1637-
error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
1634+
pstate->parse_error (msg);
16381635
}
16391636

gdb/f-exp.y

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,8 +1736,5 @@ f_language::parser (struct parser_state *par_state) const
17361736
static void
17371737
yyerror (const char *msg)
17381738
{
1739-
if (pstate->prev_lexptr)
1740-
pstate->lexptr = pstate->prev_lexptr;
1741-
1742-
error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
1739+
pstate->parse_error (msg);
17431740
}

gdb/go-exp.y

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,8 +1545,5 @@ go_language::parser (struct parser_state *par_state) const
15451545
static void
15461546
yyerror (const char *msg)
15471547
{
1548-
if (pstate->prev_lexptr)
1549-
pstate->lexptr = pstate->prev_lexptr;
1550-
1551-
error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
1548+
pstate->parse_error (msg);
15521549
}

gdb/m2-exp.y

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,8 +1006,5 @@ m2_language::parser (struct parser_state *par_state) const
10061006
static void
10071007
yyerror (const char *msg)
10081008
{
1009-
if (pstate->prev_lexptr)
1010-
pstate->lexptr = pstate->prev_lexptr;
1011-
1012-
error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
1009+
pstate->parse_error (msg);
10131010
}

gdb/p-exp.y

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,8 +1660,5 @@ pascal_language::parser (struct parser_state *par_state) const
16601660
static void
16611661
yyerror (const char *msg)
16621662
{
1663-
if (pstate->prev_lexptr)
1664-
pstate->lexptr = pstate->prev_lexptr;
1665-
1666-
error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
1663+
pstate->parse_error (msg);
16671664
}

gdb/parse.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ parser_state::push_dollar (struct stoken str)
244244
(create_internalvar (copy.c_str () + 1));
245245
}
246246

247+
/* See parser-defs.h. */
248+
249+
void
250+
parser_state::parse_error (const char *msg)
251+
{
252+
if (this->prev_lexptr)
253+
this->lexptr = this->prev_lexptr;
254+
255+
error (_("A %s in expression, near `%s'."), msg, this->lexptr);
256+
}
257+
247258

248259

249260
const char *

gdb/parser-defs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ struct parser_state : public expr_builder
262262
push (expr::make_operation<T> (std::move (lhs), std::move (rhs)));
263263
}
264264

265+
/* Function called from the various parsers' yyerror functions to throw
266+
an error. The error will include a message identifying the location
267+
of the error within the current expression. */
268+
void parse_error (const char *msg);
269+
265270
/* If this is nonzero, this block is used as the lexical context for
266271
symbol names. */
267272

0 commit comments

Comments
 (0)