[binutils-gdb] [gdb] Break up complex assignment in cp_lookup_symbol_via_imports

Tom de Vries via Gdb-cvs <[email protected]> Tue, 14 Jul 2026 08:43:20 +0000 (GMT)
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D3288b2db63ce=
1a747fbe6dd948379be1be8b86e6

commit 3288b2db63ce1a747fbe6dd948379be1be8b86e6
Author: Tom de Vries <[email protected]>
Date:   Tue Jul 14 10:43:14 2026 +0200

    [gdb] Break up complex assignment in cp_lookup_symbol_via_imports
   =20
    In cp_lookup_symbol_via_imports, we have a complex assignment:
    ...
          directive_match =3D (search_parents
                            ? (startswith (scope, current->import_dest)
                               && (len =3D=3D 0
                                   || scope[len] =3D=3D ':'
                                   || scope[len] =3D=3D '\0'))
                            : streq (scope, current->import_dest));
    ...
   =20
    Writing it like this makes it:
    - harder to comment on parts of the expression, and also
    - harder to understand and modify it.
   =20
    Also, len =3D=3D 0 makes the startswith redundant, so that part of the =
expression
    can be hoisted.  Doing so makes it clear that scope is not compared aga=
inst in
    all cases.
   =20
    Fix this by breaking this up into three separate assignments:
    ...
          if (search_parents)
            {
              if (len =3D=3D 0)
                directive_match =3D true;
              else
                directive_match =3D (startswith (scope, current->import_des=
t)
                                   && (scope[len] =3D=3D ':'
                                       || scope[len] =3D=3D '\0'));
            }
          else
            directive_match =3D streq (scope, current->import_dest);
    ...
   =20
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 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 073095f76f1..87c491d821b 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -395,7 +395,6 @@ cp_lookup_symbol_via_imports (const char *scope,
 {
   struct block_symbol sym =3D {};
   int len;
-  int directive_match;
=20
   /* 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 =3D strlen (current->import_dest);
-      directive_match =3D (search_parents
-			 ? (startswith (scope, current->import_dest)
-			    && (len =3D=3D 0
-				|| scope[len] =3D=3D ':'
-				|| scope[len] =3D=3D '\0'))
-			 : streq (scope, current->import_dest));
+
+      bool directive_match;
+      if (search_parents)
+	{
+	  if (len =3D=3D 0)
+	    directive_match =3D true;
+	  else
+	    directive_match =3D (startswith (scope, current->import_dest)
+			       && (scope[len] =3D=3D ':'
+				   || scope[len] =3D=3D '\0'));
+	}
+      else
+	directive_match =3D streq (scope, current->import_dest);
=20
       /* If the import destination is the current scope or one of its
 	 ancestors then it is applicable.  */