diff --git a/gdb/language.c b/gdb/language.c index 034086d..abfed40 100644 --- a/gdb/language.c +++ b/gdb/language.c @@ -981,6 +981,17 @@ language_bool_type (const struct language_defn *la, return ld->arch_info[la->la_language].bool_type_default; } +/* 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; +} + struct type * language_lookup_primitive_type_by_name (const struct language_defn *la, struct gdbarch *gdbarch, @@ -989,14 +1000,44 @@ language_lookup_primitive_type_by_name (const struct language_defn *la, struct language_gdbarch *ld = gdbarch_data (gdbarch, language_gdbarch_data); struct type *const *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, happen if one does gdb.lookup_type(' unsigned long ') in + Python. */ + for (i = 0, j = 0; i < len; i++) + { + if (whitespace_p (name[i])) + { + if (j == 0 || clean_name[j - 1] == ' ') + continue; + } + + if (whitespace_p (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 = ld->arch_info[la->la_language].primitive_type_vector; (*p) != NULL; p++) { - if (strcmp (TYPE_NAME (*p), name) == 0) - return (*p); + if (strcmp (TYPE_NAME (*p), clean_name) == 0) + { + xfree (clean_name); + return (*p); + } } + xfree (clean_name); + return (NULL); } diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp index 6b61f48..c9388ed 100644 --- a/gdb/testsuite/gdb.python/py-type.exp +++ b/gdb/testsuite/gdb.python/py-type.exp @@ -264,3 +264,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"