From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29427 invoked by alias); 18 Aug 2017 23:52:01 -0000 Mailing-List: contact jit-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Subscribe: Sender: jit-owner@gcc.gnu.org Received: (qmail 25617 invoked by uid 89); 18 Aug 2017 23:51:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.99.2 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=HTo:U*jit X-Spam-Status: No, score=-25.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on sourceware.org X-Spam-Level: X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 18 Aug 2017 23:51:57 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2AC0580B22; Fri, 18 Aug 2017 23:51:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2AC0580B22 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=dmalcolm@redhat.com Received: from c64.redhat.com (ovpn-112-20.phx2.redhat.com [10.3.112.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id B50F1619D7; Fri, 18 Aug 2017 23:51:54 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org, jit@gcc.gnu.org Cc: David Malcolm Subject: [committed] jit: make simpler reproducers Date: Sun, 01 Jan 2017 00:00:00 -0000 Message-Id: <1503102437-50221-1-git-send-email-dmalcolm@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 18 Aug 2017 23:51:56 +0000 (UTC) X-IsSubscribed: yes X-SW-Source: 2017-q3/txt/msg00012.txt.bz2 The C reproducers generated by gcc_jit_context_dump_reproducer_to_file contain numerous pointer values (from %p) to ensure uniqueness of the identifiers, but this makes them less readable than they could be. This patch updates reproducer::make_identifier so that the pointer is only added if it's necessary for uniqueness. Successfully bootstrapped®rtested on x86_64-pc-linux-gnu. Committed to trunk as r251191. gcc/jit/ChangeLog: * jit-recording.c (class gcc::jit::reproducer): Rename field "m_identifiers" to "m_map_memento_to_identifier". Add field "m_set_identifiers" and struct hash_traits for it. (gcc::jit::reproducer::reproducer): Update for above. (convert_to_identifier): New function. (gcc::jit::reproducer::ensure_identifier_is_unique): New method. (gcc::jit::reproducer::make_identifier): Avoid appending the %p unless necessary for uniqueness. Update for field renaming. (gcc::jit::reproducer::get_identifier): Update for field renaming. --- gcc/jit/jit-recording.c | 62 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/gcc/jit/jit-recording.c b/gcc/jit/jit-recording.c index ea4ebb1..0e7f46e0 100644 --- a/gcc/jit/jit-recording.c +++ b/gcc/jit/jit-recording.c @@ -236,7 +236,16 @@ class reproducer : public dump GNU_PRINTF(2, 3); private: - hash_map m_identifiers; + const char * ensure_identifier_is_unique (const char *candidate, void *ptr); + + private: + hash_map m_map_memento_to_identifier; + + struct hash_traits : public string_hash + { + static void remove (const char *) {} + }; + hash_set m_set_identifiers; allocator m_allocator; }; @@ -245,7 +254,8 @@ class reproducer : public dump reproducer::reproducer (recording::context &ctxt, const char *filename) : dump (ctxt, filename, 0), - m_identifiers (), + m_map_memento_to_identifier (), + m_set_identifiers (), m_allocator () { } @@ -286,6 +296,35 @@ reproducer::write_args (const vec &contexts) } } +/* Ensure that STR is a valid C identifier by overwriting + any invalid chars in-place with underscores. + + This doesn't special-case the first character. */ + +static void +convert_to_identifier (char *str) +{ + for (char *p = str; *p; p++) + if (!ISALNUM (*p)) + *p = '_'; +} + +/* Given CANDIDATE, a possible C identifier for use in a reproducer, + ensure that it is unique within the generated source file by + appending PTR to it if necessary. Return the resulting string. + + The reproducer will eventually clean up the buffer in its dtor. */ + +const char * +reproducer::ensure_identifier_is_unique (const char *candidate, void *ptr) +{ + if (m_set_identifiers.contains (candidate)) + candidate = m_allocator.xstrdup_printf ("%s_%p", candidate, ptr); + gcc_assert (!m_set_identifiers.contains (candidate)); + m_set_identifiers.add (candidate); + return candidate; +} + /* Generate a C identifier for the given memento, associating the generated buffer with the memento (for future calls to get_identifier et al). @@ -293,21 +332,20 @@ reproducer::write_args (const vec &contexts) const char * reproducer::make_identifier (recording::memento *m, const char *prefix) { - char *result; + const char *result; if (strlen (m->get_debug_string ()) < 100) { - result = m_allocator.xstrdup_printf ("%s_%s_%p", - prefix, - m->get_debug_string (), - (void *) m); - for (char *p = result; *p; p++) - if (!ISALNUM (*p)) - *p = '_'; + char *buf = m_allocator.xstrdup_printf ("%s_%s", + prefix, + m->get_debug_string ()); + convert_to_identifier (buf); + result = buf; } else result = m_allocator.xstrdup_printf ("%s_%p", prefix, (void *) m); - m_identifiers.put (m, result); + result = ensure_identifier_is_unique (result, m); + m_map_memento_to_identifier.put (m, result); return result; } @@ -350,7 +388,7 @@ reproducer::get_identifier (recording::memento *m) if (!loc->created_by_user ()) return "NULL"; - const char **slot = m_identifiers.get (m); + const char **slot = m_map_memento_to_identifier.get (m); if (!slot) { get_context ().add_error (NULL, -- 1.8.5.3