gdb: remove skip_quoted and skip_quoted_chars

The function skip_quoted_chars (completer.c) is only used by
skip_quoted (also completer.c), so could be made static.  The function
skip_quoted just calls directly to skip_quoted_chars but fills in some
default arguments.

The function skip_quoted is only used by the Pascal expression parser,
and is only used in one place.

The skip_quoted_chars function skips a single string; it either looks
for a string between matching quotes, or for a string up to a word
break character.

However, given how the Pascal expression parser calls this function,
we know that the first character will always be a single quote, in
which case skip_quoted_chars will looks for a string between matching
single quotes.

The skip_quoted_chars doesn't do any escaped character handling, it
will just stop at the next single quote character.

In this commit I propose to remove skip_quoted and skip_quoted_chars,
and replace these with a smaller function pascal_skip_string  which
I've placed in p-exp.y.  This new function only skips a string between
matching single quotes, which is exactly the use case that we need.

The benefit of this change is to remove (some) code duplication.  It
feels like skip_quoted is similar in some ways to
extract_string_maybe_quoted, however, there are some differences;
skip_quoted uses the quotes and word break characters from the
completion engine which extract_string_maybe_quoted does not.

However, I'm currently working on improving filename completion, one
part of this is that I'm looking at allowing filenames to be quoted
with single or double quotes, while the default string quoting in
GDB (for expressions) can only use single quotes.  If I do end up
allowing single and double quotes in some cases, but we retain the
single quotes only for expressions then skip_quoted starts to become a
problem, should it accept both quote types, or only one?

But given how skip_quoted is used, I can avoid worrying about this by
simply removing skip_quoted.

The Pascal tests do still pass.  The code that called skip_quoted is
called at least once in the Pascal tests (adding an abort() call
causes gdb.pascal/types.exp to fail), but I doubt the testing is
extensive.  Not sure how widely used GDB for Pascal actually is
though.
This commit is contained in:
Andrew Burgess 2024-01-15 15:55:28 +00:00
parent 7879fba359
commit 17640f65fc
3 changed files with 25 additions and 61 deletions

View File

@ -2371,61 +2371,6 @@ gdb_rl_attempted_completion_function (const char *text, int start, int end)
return NULL;
}
/* Skip over the possibly quoted word STR (as defined by the quote
characters QUOTECHARS and the word break characters BREAKCHARS).
Returns pointer to the location after the "word". If either
QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
completer. */
const char *
skip_quoted_chars (const char *str, const char *quotechars,
const char *breakchars)
{
char quote_char = '\0';
const char *scan;
if (quotechars == NULL)
quotechars = gdb_completer_quote_characters;
if (breakchars == NULL)
breakchars = current_language->word_break_characters ();
for (scan = str; *scan != '\0'; scan++)
{
if (quote_char != '\0')
{
/* Ignore everything until the matching close quote char. */
if (*scan == quote_char)
{
/* Found matching close quote. */
scan++;
break;
}
}
else if (strchr (quotechars, *scan))
{
/* Found start of a quoted string. */
quote_char = *scan;
}
else if (strchr (breakchars, *scan))
{
break;
}
}
return (scan);
}
/* Skip over the possibly quoted word STR (as defined by the quote
characters and word break characters used by the completer).
Returns pointer to the location after the "word". */
const char *
skip_quoted (const char *str)
{
return skip_quoted_chars (str, NULL, NULL);
}
/* Return a message indicating that the maximum number of completions
has been reached and that there may be more. */

View File

@ -662,11 +662,6 @@ extern void complete_expression (completion_tracker &tracker,
extern void complete_nested_command_line (completion_tracker &tracker,
const char *text);
extern const char *skip_quoted_chars (const char *, const char *,
const char *);
extern const char *skip_quoted (const char *);
/* Called from command completion function to skip over /FMT
specifications, allowing the rest of the line to be completed. Returns
true if the /FMT is at the end of the current line and there is nothing

View File

@ -76,6 +76,8 @@ static void yyerror (const char *);
static char *uptok (const char *, int);
static const char *pascal_skip_string (const char *str);
using namespace expr;
%}
@ -1042,6 +1044,28 @@ uptok (const char *tokstart, int namelen)
return uptokstart;
}
/* Skip over a Pascal string. STR must point to the opening single quote
character. This function returns a pointer to the character after the
closing single quote character.
This function does not support embedded, escaped single quotes, which
is done by placing two consecutive single quotes into a string.
Support for this would be easy to add, but this function is only used
from the Python expression parser, and if we did skip over escaped
quotes then the rest of the expression parser wouldn't handle them
correctly. */
static const char *
pascal_skip_string (const char *str)
{
gdb_assert (*str == '\'');
do
++str;
while (*str != '\0' && *str != '\'');
return str;
}
/* Read one token, getting characters through lexptr. */
static int
@ -1120,7 +1144,7 @@ yylex (void)
c = *pstate->lexptr++;
if (c != '\'')
{
namelen = skip_quoted (tokstart) - tokstart;
namelen = pascal_skip_string (tokstart) - tokstart;
if (namelen > 2)
{
pstate->lexptr = tokstart + namelen;