]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb] Break up complex assignment in cp_lookup_symbol_via_imports
authorTom de Vries <tdevries@suse.de>
Tue, 14 Jul 2026 08:43:14 +0000 (10:43 +0200)
committerTom de Vries <tdevries@suse.de>
Tue, 14 Jul 2026 08:43:14 +0000 (10:43 +0200)
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 = true;
          else
            directive_match = (startswith (scope, current->import_dest)
                               && (scope[len] == ':'
                                   || scope[len] == '\0'));
        }
      else
        directive_match = streq (scope, current->import_dest);
...

Approved-By: Tom Tromey <tom@tromey.com>
gdb/cp-namespace.c

index 073095f76f13645fbb69870cc2e43c4336126030..87c491d821b63a3e2bde0cffd7f93b72878cbdda 100644 (file)
@@ -395,7 +395,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
@@ -443,13 +442,21 @@ cp_lookup_symbol_via_imports (const char *scope,
         do not use this directive.  */
       if (!current->valid_line (boundary_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 = true;
+         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.  */