From patchwork Wed Oct 29 18:50:39 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siva Chandra Reddy X-Patchwork-Id: 3483 Received: (qmail 26706 invoked by alias); 29 Oct 2014 18:50:44 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 26695 invoked by uid 89); 29 Oct 2014 18:50:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.1 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-ob0-f175.google.com Received: from mail-ob0-f175.google.com (HELO mail-ob0-f175.google.com) (209.85.214.175) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 29 Oct 2014 18:50:42 +0000 Received: by mail-ob0-f175.google.com with SMTP id wm4so2914506obc.34 for ; Wed, 29 Oct 2014 11:50:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-type; bh=gvr0WaslQs/p4LaURPoi96sb/g2WaATFun1CL1qRkQ0=; b=EtedGXls5J+gic3aW6ccTOtv/vCgvJwJrPJeMBHkrsGvI1lGe0pYXnaJtPZWiieEk5 ewRHJL8QR1Yll02cB4Pcm7rdsNkKTT7tSXrRre/kRe69HE5Rr2q3i1gppSibJOL+O6qi DVYR33J12YC5k2KSBLqMXhW6yRQ+EUS5C8NKDhQ/oFR9Wjhtz2EhCem2l5+x+edw6DTe ORKqE/dfOA3/9kwFsTnMoW7aP21S8/pCaS7jxaCbwrE/LdWBRVV28kQPfkBnLu3P+rN1 SJa5OqqRt3xf8Xd5/vRu0vLMB0jGbAgoLjTpkrahdxVAfc9+46oLnT7KKxPpbgsIBV34 Iw+Q== X-Gm-Message-State: ALoCoQnwqfkYpWBlGR+utllprv6MX9ZAWdmTYHqzB8siNN60QeYXXPso2klNe/1ty1PSTOTB06qD MIME-Version: 1.0 X-Received: by 10.202.170.21 with SMTP id t21mr9568856oie.5.1414608640255; Wed, 29 Oct 2014 11:50:40 -0700 (PDT) Received: by 10.202.197.13 with HTTP; Wed, 29 Oct 2014 11:50:39 -0700 (PDT) Date: Wed, 29 Oct 2014 11:50:39 -0700 Message-ID: Subject: [PATCH] Remove unwanted spaces when looking up builtin types From: Siva Chandra To: gdb-patches X-IsSubscribed: yes Currently, if we do something like gdb.lookup_type(" unsigned long "), GDB errors out because of the leading and trailing spaces. The attached patch fixes this problem. A practical situation where this problem is hit is when invoking template methods. Its not uncommon to do things like this: (gdb) p foo.bar< unsigned long >() If "bar" happens to be an xmethod, then its implementation will typically need to parse the name of the method ("bar< unsigned long >" in the above example) to get the template argument and lookup the type. GDB currently fails for such cases. One could of course sanitize/fix such inputs in Python before calling lookup_type, but I think it is better done on the GDB side as having white spaces is valid syntax. For non-builtin types, lookup_type goes through the symbol lookup path. AFAIU, that path already ignores whitespaces. gdb/ChangeLog: * language.c (language_lookup_primitive_type_by_name): Remove unwanted space in the type name before looking it up. gdb/testsuite/ChangeLog: * gdb.python/py-type.exp: Add new tests. 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"