From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 7/7] Rewrite the @selector code
Date: Sun, 16 Nov 2025 18:27:06 -0700 [thread overview]
Message-ID: <20251117012752.2657208-8-tom@tromey.com> (raw)
In-Reply-To: <20251117012752.2657208-1-tom@tromey.com>
This rewrites the Objective-C @selector implementation in c-exp.y,
following a couple general comments in the patch supplied for
bug 20503.
I suspect something else changed in the Objective-C runtime, though,
as most of the examples in that bug still do not work, even though
this series (I believe) addresses all the same points.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20503
---
gdb/c-exp.y | 53 +++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 45 insertions(+), 8 deletions(-)
diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index fadd735c517..829058027f3 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -224,7 +224,7 @@ static void c_print_token (FILE *file, int type, YYSTYPE value);
%token <tsval> STRING
%token <tsval> NSSTRING /* ObjC Foundation "NSString" literal */
-%token SELECTOR /* ObjC "@selector" pseudo-operator */
+%token <sval> SELECTOR /* ObjC "@selector" pseudo-operator */
%token <tsval> CHAR
%token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
%token <ssym> UNKNOWN_CPP_NAME
@@ -918,10 +918,10 @@ exp : DOLLAR_VARIABLE
}
;
-exp : SELECTOR '(' name ')'
+exp : SELECTOR
{
pstate->push_new<objc_selector_operation>
- (copy_name ($3));
+ (copy_name ($1));
}
;
@@ -2664,6 +2664,43 @@ static bool last_was_structop;
/* Depth of parentheses. */
static int paren_depth;
+/* Lex an Objective-C @selector. Return true if lexed. In this case,
+ sets the resulting token and updates the lex pointer. Otherwise
+ returns false and updates nothing. */
+
+static bool
+lex_selector (const char **lex_ptr, struct stoken *token)
+{
+ const char *p = *lex_ptr;
+
+ if (!startswith (p, "selector"))
+ return false;
+
+ p += strlen ("selector");
+ p = skip_spaces (p);
+ if (*p != '(')
+ return false;
+ ++p;
+
+ /* The selector name matches [A-Za-z0-9:_-]+. We could probably be
+ a bit more refined but meh. */
+ const char *start = p;
+ while (c_isalnum (*p) || *p == ':' || *p == '_' || *p == '-')
+ ++p;
+ if (p == start)
+ return false;
+ const char *end = p;
+
+ p = skip_spaces (p);
+ if (*p != ')')
+ return false;
+ ++p;
+
+ *lex_ptr = p;
+ *token = { start, (int) (end - start) };
+ return true;
+}
+
/* Read one token, getting characters through lexptr. */
static int
@@ -2872,12 +2909,11 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
if (par_state->language ()->la_language == language_objc)
{
- size_t len = strlen ("selector");
-
- if (strncmp (p, "selector", len) == 0
- && (p[len] == '\0' || c_isspace (p[len])))
+ struct stoken sel_token;
+ if (lex_selector (&p, &sel_token))
{
- pstate->lexptr = p + len;
+ pstate->lexptr = p;
+ yylval.sval = sel_token;
return SELECTOR;
}
else if (*p == '"')
@@ -3556,6 +3592,7 @@ c_print_token (FILE *file, int type, YYSTYPE value)
case NSSTRING:
case DOLLAR_VARIABLE:
+ case SELECTOR:
parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
break;
--
2.49.0
prev parent reply other threads:[~2025-11-17 1:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 1:26 [PATCH 0/7] Objective-C fixes Tom Tromey
2025-11-17 1:27 ` [PATCH 1/7] Reformat gdb.objc tests Tom Tromey
2025-11-17 1:27 ` [PATCH 2/7] Make the gdb.objc tests compile Tom Tromey
2025-11-17 10:09 ` Matt Rice
2025-11-18 0:51 ` Tom Tromey
2025-11-18 1:12 ` Matt Rice
2025-12-02 16:20 ` Tom Tromey
2025-12-03 15:24 ` Matt Rice
2025-11-17 1:27 ` [PATCH 3/7] Minor fixes to make gdb.objc tests pass Tom Tromey
2025-11-17 1:27 ` [PATCH 4/7] Rename lookup_struct_typedef Tom Tromey
2025-11-17 1:27 ` [PATCH 5/7] Remove a couple Objective-C expression helpers Tom Tromey
2025-11-17 1:27 ` [PATCH 6/7] Avoid crash with "NSString" literals Tom Tromey
2025-11-17 1:27 ` Tom Tromey [this message]
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=20251117012752.2657208-8-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/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