[PATCH v3 1/2] [gdb] Break up complex assignment in cp_lookup_symbol_via_imports

Tom de Vries
2026-04-10T12:12:31Z

In cp_lookup_symbol_via_imports, we have a complex assignment:
...
directive_match = (search_parents
? (startswith (scope, current->import_dest)
&& (len == 0
|| scope[len] == ':'
|| scope[len] == '\0'))
: streq (scope, current->import_dest));
...

Writing it like this makes it:
- harder to comment on parts of the expression, and also
- harder to understand and modify it.

Also, len == 0 makes the startswith redundant, so that part of the expression
can be hoisted. Doing so makes it clear that scope is not compared against in
all cases.

Fix this by breaking this up into three separate assignments:
...
if (search_parents)
{
if (len == 0)
directive_match = 1;
else
directive_match = (startswith (scope, current->import_dest)
&& (scope[len] == ':'
|| scope[len] == '\0'));
}
else
directive_match = streq (scope, current->import_dest);
...
---
gdb/cp-namespace.c | 21 ++++++++++++++-------
1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c
index c8cd5c245aa..f8b71be3ff1 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -394,7 +394,6 @@ cp_lookup_symbol_via_imports (const char *scope,
{
struct block_symbol sym = {};
int len;
- int directive_match;

/* All the symbols we found will be kept in this relational map between
the mangled name and the block_symbol found. We do this so that GDB
@@ -425,13 +424,21 @@ cp_lookup_symbol_via_imports (const char *scope,
do not use this directive. */
if (!current->valid_line (boundary_sal.line))
continue;
+
len = strlen (current->import_dest);
- directive_match = (search_parents
- ? (startswith (scope, current->import_dest)
- && (len == 0
- || scope[len] == ':'
- || scope[len] == '\0'))
- : streq (scope, current->import_dest));
+
+ bool directive_match;
+ if (search_parents)
+ {
+ if (len == 0)
+ directive_match = 1;
+ else
+ directive_match = (startswith (scope, current->import_dest)
+ && (scope[len] == ':'
+ || scope[len] == '\0'));
+ }
+ else
+ directive_match = streq (scope, current->import_dest);

/* If the import destination is the current scope or one of its
ancestors then it is applicable. */
--
2.51.0