[1/2] gdb/dwarf: remove unnecessary comparison in cooked_index_entry::compare

Message ID 20250324202035.3727263-1-simon.marchi@polymtl.ca
State New
Headers
Series [1/2] gdb/dwarf: remove unnecessary comparison in cooked_index_entry::compare |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Simon Marchi March 24, 2025, 8:20 p.m. UTC
  From: Simon Marchi <simon.marchi@polymtl.ca>

I believe that the `(mode == MATCH && a == munge ('<'))` part of the
condition is unnecesary.  Or perhaps I don't understand the algorithm.

The use of "munge" above effectively makes it so that the template
portion of names is completely ignored for the sake of the comparison.

Then, in the condition, this:

    a == munge ('<')

is functionally equivalent to

    a == '\0'

If `a` is indeed '\0', and `b` is also '\0', then we would have taken
the earlier branch:

    if (a == b)
      return 0;

If `b` is not '\0', then we won't take this branch and we'll go into the
final comparison:

    return a < b ? -1 : 1;

So, as far as I can see, there is no case where `mode == MATCH`, where
we're going to use this special `return 0`.

Regression tested using the various DWARF target boards on Debian 12.

Change-Id: I5ea0463c1fdbbc1b003de2f0a423fd0073cc9dec
---
 gdb/dwarf2/cooked-index.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)


base-commit: 49d5323352a910dd99cdd635c4923610cce36d10
  

Comments

Tom Tromey March 25, 2025, 1:42 p.m. UTC | #1
>>>>> "Simon" == simon marchi <simon.marchi@polymtl.ca> writes:

Simon> If `a` is indeed '\0', and `b` is also '\0', then we would have taken
Simon> the earlier branch:
Simon>     if (a == b)
Simon>       return 0;

Yeah, I agree.

I think I probably just didn't update this condition when fixing the
"templates.exp" problem.

Approved-By: Tom Tromey <tom@tromey.com>

Tom
  

Patch

diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c
index 724615f497e3..1a2e19debab5 100644
--- a/gdb/dwarf2/cooked-index.c
+++ b/gdb/dwarf2/cooked-index.c
@@ -122,11 +122,8 @@  cooked_index_entry::compare (const char *stra, const char *strb,
 
   /* When completing, if STRB ends earlier than STRA, consider them as
      equal.  */
-  if (mode == COMPLETE || (mode == MATCH && a == munge ('<')))
-    {
-      if (b == '\0')
-	return 0;
-    }
+  if (mode == COMPLETE && b == '\0')
+    return 0;
 
   return a < b ? -1 : 1;
 }