Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH v2] Rewrite the Ada lexer
Date: Thu, 19 Nov 2026 00:00:37 -0500	[thread overview]
Message-ID: <ba17c01e-cb6a-4766-9eec-74ae610ec7d2@simark.ca> (raw)
In-Reply-To: <20261117191854.2893194-1-tromey@adacore.com>

On 11/17/26 2:18 PM, Tom Tromey wrote:
> Recently a series landed that changed how the Ada lexer was built.  In
> particular, commit d3d68d79 ("gdb: let the parser and lexer generators
> prefix their symbols") changed gdb to require a newer version of flex.
> 
> Unfortunately, this version of flex is reported as being difficult to
> build with mingw; and this change broke all the Windows-hosted builds
> at AdaCore.
> 
> Rather than try to puzzle out the flex build, I decided to rewrite the
> lexer to avoid the flex dependency.  This patch is the result.  I
> tried for a fairly minimal transition, so some oddities are preserved.
> Further cleanups are possible (I have a few I will send later).
> 
> I've also added some unit tests.  In some cases these test things that
> previously were not tested in the tree.
> 
> v2: addressed review comments.

LGTM, I noted some minor comments below.

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Some cleanup that could be part of this patch, of a later one:

 - remove ada-lex-gen.{c,h} from .gitignore
 - post-process-parser-output.sh can be simplified
 - ECHO_LEX can be removed from silent-rules.mk

> +/* The number of subexpressions to allocate space for, including the
> +   "0th" whole match subexpression.  */
> +#define NUM_OPER_SUBEXPRESSIONS 2

NUM_OPER_SUBEXPRESSIONS is unused, I don't know if it should?

> @@ -207,51 +275,47 @@ processReal (struct parser_state *par_state, const char *num0)
>  /* See ada-exp-parser.h.  */
>  
>  struct stoken
> -processId (const char *name0, int len)
> +ada_parse_state::process_id (std::string_view name0)
>  {
> -  char *name = (char *) obstack_alloc (&ada_parser->temp_space, len + 11);
> -  int i0, i;
> +  char *name = (char *) obstack_alloc (&ada_parser->temp_space,
> +				       name0.size () + 11);
>    struct stoken result;
>  
>    result.ptr = name;
> -  while (len > 0 && c_isspace (name0[len-1]))
> -    len -= 1;
> +  while (!name0.empty () && c_isspace (name0.back ()))
> +    name0 = name0.substr (0, name0.size () - 1);

Can use name0.remove_suffix(1).

> @@ -365,39 +406,32 @@ attributes[] = {
>  /* See ada-exp-parser.h.  */
>  
>  int
> -processAttribute (const char *str)
> +ada_parse_state::process_attribute (const std::string &str, bool completing)
>  {
> -  gdb_assert (*str == '\'');
> -  ++str;
> -  while (c_isspace (*str))
> -    ++str;
> -
> -  int len = strlen (str);
> -  if (len > 0 && str[len - 1] == COMPLETE_CHAR)
> +  size_t len = str.length ();
> +  if (completing)
>      {
> -      /* This is enforced by YY_INPUT.  */
> -      gdb_assert (pstate->parse_completion);
>        ada_yylval.sval.ptr = obstack_strndup (&ada_parser->temp_space,

Should be just temp_space (or this->temp_space), instead of going
through the `ada_parser` global.  There is another instance at line 280
in ada_parse_state::process_id.

> +ada_parse_state::lex_number ()
> +{
> +  const char *num_start = pstate->lexptr;
> +  gdb_assert (c_isdigit (*num_start));
> +
> +  regmatch_t subexps[NUM_SUBEXPRESSIONS];
> +  int match = regexec (&number_regex, num_start, ARRAY_SIZE (subexps),
> +		       subexps, 0);
> +  /* Failure means the regexp is broken.  */
> +  gdb_assert (match == 0);
> +
> +  pstate->lexptr += subexps[0].rm_eo;
> +
> +#define NUMERAL_WIDTH 256
> +  char numbuf[NUMERAL_WIDTH];

This predates the patch, so maybe a TODO cleanup, we can overflow this
static buffer:

  (gdb) set language ada
  (gdb) python gdb.execute("print " + "1" * 300)

It would easily be fixed by making canonicalize_numeral return an
std::string.  Apparently, the callers can keep using their trick of
splitting the string with \0 in place, in the std::string:

  char *e_ptr = strrchr (numbuf.data (), 'e');
  *e_ptr = '\0';

> +ada_parse_state::invalid_char ()
>  {
> -  pstate->lexptr -= ada_yyleng;
> -  while (c_toupper (*pstate->lexptr) != c_toupper (ch))
> -    pstate->lexptr -= 1;
> -  ada_yyrestart (NULL);
> +  error (_("Invalid character '%s' in expression."), pstate->lexptr);

%s or %c?

> +  const code_entry us3_test[] =
> +    {
> +     { "x", NAME },
> +      { nullptr, '+' },
> +      { "a___b", NAME },
> +      { nullptr, 0 }

Indentation is off for the first element.

> @@ -1349,3 +2152,19 @@ ada_parse (struct parser_state *par_state)
>      }
>    return result;
>  }
> +
> +INIT_GDB_FILE (ada_exp_parser)
> +{
> +  int code = regcomp (&number_regex, number_regex_text,
> +		      REG_EXTENDED | REG_ICASE);
> +  /* If the regular expression was incorrect, it was a programming
> +     error.  */
> +  gdb_assert (code == 0);
> +  code = regcomp (&operator_regex, operator_regex_text,
> +		  REG_EXTENDED | REG_ICASE);
> +  gdb_assert (code == 0);

Can these use compiled_regex?

Simon

  parent reply	other threads:[~2026-11-19  5:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 19:18 Tom Tromey
2026-11-18 11:03 ` Tom de Vries
2026-11-18 15:58   ` Tom Tromey
2026-11-19  5:00 ` Simon Marchi [this message]
2026-11-19 16:59   ` Tom Tromey
2026-11-19 17:31   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ba17c01e-cb6a-4766-9eec-74ae610ec7d2@simark.ca \
    --to=simark@simark.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox