[v2,1/3] Move find_toplevel_char to utils.[ch]

Message ID 20200420215708.42314-2-keiths@redhat.com
State New
Headers show
Series
  • Completer C++ template support
Related show

Commit Message

Simon Marchi via Gdb-patches April 20, 2020, 9:57 p.m.
find_toplevel_char is being used more and more outside of linespec.c, so
this patch moves it into utils.[ch].

This patch was previously approved:
https://sourceware.org/pipermail/gdb-patches/2018-February/146819.html

gdb/ChangeLog:

	* linespec.c (find_toplevel_char): Moved to ...
	* utils.c (find_toplevel_char): ... here.
	* linespec.h (find_toplevel_char): Moved to ...
	* utils.h (find_toplevel_char): ... here.
---
 gdb/linespec.c | 77 --------------------------------------------------
 gdb/linespec.h |  7 -----
 gdb/utils.c    | 74 ++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/utils.h    |  7 +++++
 4 files changed, 81 insertions(+), 84 deletions(-)

-- 
2.25.2

Comments

Tom Tromey April 24, 2020, 7:57 p.m. | #1
>>>>> "Keith" == Keith Seitz via Gdb-patches <gdb-patches@sourceware.org> writes:


Keith> find_toplevel_char is being used more and more outside of linespec.c, so
Keith> this patch moves it into utils.[ch].

Keith> This patch was previously approved:
Keith> https://sourceware.org/pipermail/gdb-patches/2018-February/146819.html

Keith> gdb/ChangeLog:

Keith> 	* linespec.c (find_toplevel_char): Moved to ...
Keith> 	* utils.c (find_toplevel_char): ... here.
Keith> 	* linespec.h (find_toplevel_char): Moved to ...
Keith> 	* utils.h (find_toplevel_char): ... here.

If this is C++-specific, maybe cp-support.c would be a better spot?
What do you think?

Tom

Patch

diff --git a/gdb/linespec.c b/gdb/linespec.c
index d853e02d8f..0dcd6bba4b 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -1274,83 +1274,6 @@  find_methods (struct type *t, enum language t_lang, const char *name,
     superclasses->push_back (TYPE_BASECLASS (t, ibase));
 }
 
-/* Find an instance of the character C in the string S that is outside
-   of all parenthesis pairs, single-quoted strings, and double-quoted
-   strings.  Also, ignore the char within a template name, like a ','
-   within foo<int, int>, while considering C++ operator</operator<<.  */
-
-const char *
-find_toplevel_char (const char *s, char c)
-{
-  int quoted = 0;		/* zero if we're not in quotes;
-				   '"' if we're in a double-quoted string;
-				   '\'' if we're in a single-quoted string.  */
-  int depth = 0;		/* Number of unclosed parens we've seen.  */
-  const char *scan;
-
-  for (scan = s; *scan; scan++)
-    {
-      if (quoted)
-	{
-	  if (*scan == quoted)
-	    quoted = 0;
-	  else if (*scan == '\\' && *(scan + 1))
-	    scan++;
-	}
-      else if (*scan == c && ! quoted && depth == 0)
-	return scan;
-      else if (*scan == '"' || *scan == '\'')
-	quoted = *scan;
-      else if (*scan == '(' || *scan == '<')
-	depth++;
-      else if ((*scan == ')' || *scan == '>') && depth > 0)
-	depth--;
-      else if (*scan == 'o' && !quoted && depth == 0)
-	{
-	  /* Handle C++ operator names.  */
-	  if (strncmp (scan, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0)
-	    {
-	      scan += CP_OPERATOR_LEN;
-	      if (*scan == c)
-		return scan;
-	      while (isspace (*scan))
-		{
-		  ++scan;
-		  if (*scan == c)
-		    return scan;
-		}
-	      if (*scan == '\0')
-		break;
-
-	      switch (*scan)
-		{
-		  /* Skip over one less than the appropriate number of
-		     characters: the for loop will skip over the last
-		     one.  */
-		case '<':
-		  if (scan[1] == '<')
-		    {
-		      scan++;
-		      if (*scan == c)
-			return scan;
-		    }
-		  break;
-		case '>':
-		  if (scan[1] == '>')
-		    {
-		      scan++;
-		      if (*scan == c)
-			return scan;
-		    }
-		  break;
-		}
-	    }
-	}
-    }
-
-  return 0;
-}
-
 /* The string equivalent of find_toplevel_char.  Returns a pointer
    to the location of NEEDLE in HAYSTACK, ignoring any occurrences
    inside "()" and "<>".  Returns NULL if NEEDLE was not found.  */
diff --git a/gdb/linespec.h b/gdb/linespec.h
index 2a34f60f05..68be1d4467 100644
--- a/gdb/linespec.h
+++ b/gdb/linespec.h
@@ -162,13 +162,6 @@  extern const char *get_gdb_linespec_parser_quote_characters (void);
 
 extern int is_ada_operator (const char *string);
 
-/* Find an instance of the character C in the string S that is outside
-   of all parenthesis pairs, single-quoted strings, and double-quoted
-   strings.  Also, ignore the char within a template name, like a ','
-   within foo<int, int>.  */
-
-extern const char *find_toplevel_char (const char *s, char c);
-
 /* Find the end of the (first) linespec pointed to by *STRINGP.
    STRINGP will be advanced to this point.  */
 
diff --git a/gdb/utils.c b/gdb/utils.c
index bda6bbf5b0..3153e2535a 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3344,6 +3344,80 @@  strip_leading_path_elements (const char *path, int n)
   return p;
 }
 
+/* See description in utils.h.  */
+
+const char *
+find_toplevel_char (const char *s, char c)
+{
+  int quoted = 0;		/* zero if we're not in quotes;
+				   '"' if we're in a double-quoted string;
+				   '\'' if we're in a single-quoted string.  */
+  int depth = 0;		/* Number of unclosed parens we've seen.  */
+  const char *scan;
+
+  for (scan = s; *scan; scan++)
+    {
+      if (quoted)
+	{
+	  if (*scan == quoted)
+	    quoted = 0;
+	  else if (*scan == '\\' && *(scan + 1))
+	    scan++;
+	}
+      else if (*scan == c && ! quoted && depth == 0)
+	return scan;
+      else if (*scan == '"' || *scan == '\'')
+	quoted = *scan;
+      else if (*scan == '(' || *scan == '<')
+	depth++;
+      else if ((*scan == ')' || *scan == '>') && depth > 0)
+	depth--;
+      else if (*scan == 'o' && !quoted && depth == 0)
+	{
+	  /* Handle C++ operator names.  */
+	  if (strncmp (scan, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0)
+	    {
+	      scan += CP_OPERATOR_LEN;
+	      if (*scan == c)
+		return scan;
+	      while (isspace (*scan))
+		{
+		  ++scan;
+		  if (*scan == c)
+		    return scan;
+		}
+	      if (*scan == '\0')
+		break;
+
+	      switch (*scan)
+		{
+		  /* Skip over one less than the appropriate number of
+		     characters: the for loop will skip over the last
+		     one.  */
+		case '<':
+		  if (scan[1] == '<')
+		    {
+		      scan++;
+		      if (*scan == c)
+			return scan;
+		    }
+		  break;
+		case '>':
+		  if (scan[1] == '>')
+		    {
+		      scan++;
+		      if (*scan == c)
+			return scan;
+		    }
+		  break;
+		}
+	    }
+	}
+    }
+
+  return 0;
+}
+
 /* See utils.h.  */
 
 void
diff --git a/gdb/utils.h b/gdb/utils.h
index 3434ff1caa..84f07bc863 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -589,4 +589,11 @@  fast_hash (const void *ptr, size_t len, unsigned int start_value = 0)
 #endif
 }
 
+/* Find an instance of the character C in the string S that is outside
+   of all parenthesis pairs, single-quoted strings, and double-quoted
+   strings.  Also, ignore the char within a template name, like a ','
+   within foo<int, int>.  */
+
+extern const char *find_toplevel_char (const char *s, char c);
+
 #endif /* UTILS_H */