[v3] Remove unwanted spaces when looking up builtin types

Message ID CAGyQ6gzQBM-_sCeGOXZE8uQfrOGDcWYt=QjaHX5E4gd+rCzvaw@mail.gmail.com
State New, archived
Headers

Commit Message

Siva Chandra Reddy Jan. 7, 2015, 2:35 p.m. UTC
  On Tue, Jan 6, 2015 at 11:03 PM, Joel Brobecker <brobecker@adacore.com> wrote:
>> +/* Return 1 if C is a whitespace character, 0 otherwise.  */
>> +
>> +static int
>> +whitespace_p (const char c)
>> +{
>> +  if (c == ' ' || c == '\n' || c == '\t')
>> +    return 1;
>> +  else
>> +    return 0;
>
> Why not using isspace directly? Apologies if this was explained
> before, but then, a comment would be helpful, and maybe a more
> specific function name.  My first reaction to this kind of routine
> was that this should be in one of our "utils" files, so as to allow
> other parts of the code to use it as well.

To be honest, I did not know about the existence of "isspace". I have
removed whitespace_p now from my patch. The v3 is attached.

2015-01-07  Siva Chandra Reddy  <sivachandra@google.com>

gdb/ChangeLog:

        * language.c (language_lookup_primitive_type_1): Remove
        unwanted space in the type name before looking it up.

gdb/testsuite/ChangeLog:

        * gdb.python/py-type.exp: Add new tests.
  

Patch

diff --git a/gdb/language.c b/gdb/language.c
index 1e6b983..0e6d241 100644
--- a/gdb/language.c
+++ b/gdb/language.c
@@ -994,13 +994,39 @@  language_lookup_primitive_type_1 (const struct language_arch_info *lai,
 				  const char *name)
 {
   struct type **p;
+  int len = strlen (name);
+  int i, j;
+  char *clean_name = (char *) xmalloc (sizeof (char) * len + 1);
+
+  /* Remove unwanted whitespace in the typename.  This could happen, for
+     example, if one does gdb.lookup_type(' unsigned long ') in Python.  */
+  for (i = 0, j = 0; i < len; i++)
+    {
+      if (isspace (name[i]))
+	{
+	  if (j == 0 || clean_name[j - 1] == ' ')
+	    continue;
+	}
+
+      if (isspace (name[i]))
+	clean_name[j] = ' ';
+      else
+	clean_name[j] = name[i];
+
+      j++;
+    }
+  if (j > 0 && clean_name[j - 1] == ' ')
+    j--;
+  clean_name[j] = '\0';
 
   for (p = lai->primitive_type_vector; (*p) != NULL; p++)
     {
-      if (strcmp (TYPE_NAME (*p), name) == 0)
-	return p;
+      if (strcmp (TYPE_NAME (*p), clean_name) == 0)
+	break;
     }
-  return NULL;
+  xfree (clean_name);
+
+  return p;
 }
 
 /* See language.h.  */
diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp
index c4c8d9f..0c45ec9 100644
--- a/gdb/testsuite/gdb.python/py-type.exp
+++ b/gdb/testsuite/gdb.python/py-type.exp
@@ -270,3 +270,15 @@  with_test_prefix "lang_cpp" {
     test_template
     test_enums
 }
+
+# Tests to lookup builtin types
+gdb_test "python print gdb.lookup_type ('unsigned int')" "unsigned int" \
+    "lookup unsigned int"
+gdb_test "python print gdb.lookup_type (' unsigned long ')" "unsigned long" \
+    "lookup unsigned long"
+gdb_test "python print gdb.lookup_type (' unsigned  char ')" "unsigned char" \
+    "lookup unsigned char"
+gdb_test "python print gdb.lookup_type (' unsigned\\n char ')" "unsigned char" \
+    "lookup unsigned char"
+gdb_test "python print gdb.lookup_type (' unsigned\\tlong ')" "unsigned long" \
+    "lookup unsigned long"