diff options
author | David Malcolm <dmalcolm@redhat.com> | 2017-08-18 23:50:34 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2017-08-18 23:50:34 +0000 |
commit | 519d0798689cc68bfb27658c6f91ac3e07c04517 (patch) | |
tree | cc81ac395b2f465a9ba04bcf927d4703f1e0d7e6 /gcc/jit | |
parent | b1eb28d14049e19baf772e6052a3cc388e85bd51 (diff) | |
download | gcc-519d0798689cc68bfb27658c6f91ac3e07c04517.zip gcc-519d0798689cc68bfb27658c6f91ac3e07c04517.tar.gz gcc-519d0798689cc68bfb27658c6f91ac3e07c04517.tar.bz2 |
jit: make simpler reproducers
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.
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.
From-SVN: r251191
Diffstat (limited to 'gcc/jit')
-rw-r--r-- | gcc/jit/ChangeLog | 12 | ||||
-rw-r--r-- | gcc/jit/jit-recording.c | 62 |
2 files changed, 62 insertions, 12 deletions
diff --git a/gcc/jit/ChangeLog b/gcc/jit/ChangeLog index 66e34b6..824a4a5 100644 --- a/gcc/jit/ChangeLog +++ b/gcc/jit/ChangeLog @@ -1,3 +1,15 @@ +2017-08-18 David Malcolm <dmalcolm@redhat.com> + + * 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. + 2017-08-09 David Malcolm <dmalcolm@redhat.com> * docs/cp/topics/types.rst (Vector types): New section. 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<recording::memento *, const char *> m_identifiers; + const char * ensure_identifier_is_unique (const char *candidate, void *ptr); + + private: + hash_map<recording::memento *, const char *> m_map_memento_to_identifier; + + struct hash_traits : public string_hash + { + static void remove (const char *) {} + }; + hash_set<const char *, hash_traits> 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 <recording::context *> &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 <recording::context *> &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, |