[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gcc-fortran
Subject:    [gfortran, committed] Cleanup gfc_find_gsymbol
From:       Tobias_Schlüter <tobias.schlueter () physik ! uni-muenchen ! de>
Date:       2007-03-29 23:54:29
Message-ID: 460C51B5.4020700 () physik ! uni-muenchen ! de
[Download RAW message or body]

Hi,

my previous patch reminded me that we're still using strcmp to establish 
the ordering in our binary tree of symbols instead of pointer 
comparisons.  I refrained from fixing this to use pointer comparisons, 
because I didn't want to dive into the wondrous world that is the C 
standard to find out, if --or how-- one can assume any ordering relation 
between pointers, necessary to build up a binary tree.

Anyway, looking over the code I spotted that gfc_find_gsymbol 
unnecessarily descends into both branches of the global symbol tree, so 
I fixed it to do The Right Thing.

Committed as r123355 under the obviously correct rule after the 
obligatory testing on i386-darwin.

Cheers,
- Tobi


["gsymbol.diff.txt" (text/plain)]

2007-03-30  Tobias Schlüter  <tobi@gcc.gnu.org>

	* symbol.c (gfc_find_gsymbol): Simplify, don't unconditionally
	descend into all branches.

Index: symbol.c
===================================================================
--- symbol.c	(revision 123338)
+++ symbol.c	(working copy)
@@ -2770,20 +2770,19 @@ gfc_symbol_state(void) {
 gfc_gsymbol *
 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
 {
-  gfc_gsymbol *s;
+  int c;
 
   if (symbol == NULL)
     return NULL;
-  if (strcmp (symbol->name, name) == 0)
-    return symbol;
 
-  s = gfc_find_gsymbol (symbol->left, name);
-  if (s != NULL)
-    return s;
+  while (symbol)
+    {
+      c = strcmp (name, symbol->name);
+      if (!c)
+	return symbol;
 
-  s = gfc_find_gsymbol (symbol->right, name);
-  if (s != NULL)
-    return s;
+      symbol = (c < 0) ? symbol->left : symbol->right;
+    }
 
   return NULL;
 }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic