Rust-bindgen: Add `--emit-ir-graphviz=output.dot` to dump a graphviz dot file

Created on 6 Feb 2017  路  10Comments  路  Source: rust-lang/rust-bindgen

Shouldn't be too hard, and I often wish I had this when trying to debug anything involving resolved type references and the like.

There is a crate: https://github.com/GrahamDennis/dot-rust

Here is an intro to dot: http://graphviz.org/pdf/dotguide.pdf

If we didn't want to bring in an extra dependency, it shouldn't be hard to just write text to a file manually.

We would dump each item's attributes as an HTML table in the label for the item, and use TypeCollector to draw edges between items.

Here is a sketch on what an implementation might look like:

let mut dot_file = try!(open_the_specified_file_for_writing());
try!(writeln!(&mut dot_file, "digraph {"));

for (id, item) in ctx.items() {
    try!(writeln!(&mut dot_file, "{} {};", id.0, item.dot_attributes()));

    let mut edges = ItemSet::new();
    item.collect_types(ctx, &mut edges);

    for sub_id in edges {
        try!(writeln!(&mut dot_file, "{} -> {};", id, sub_id));
    }
}

try!(writeln!(&mut dot_file, "}"));

Output should look something like this (we can add more table rows incrementally):

digraph {
    1 [fontname="courier", label=<
       <table border="0">
       <tr><td>ItemId(1)</td></tr>
       <tr><td>name</td><td>Foo</td></tr>
       <tr><td>kind</td><td>Type</td></tr>
       </table>
       >];
    1 -> 2;
    1 -> 3;

    2 [fontname="courier", label=<
       <table border="0">
       <tr><td>ItemId(2)</td></tr>
       <tr><td>name</td><td>Bar</td></tr>
       <tr><td>kind</td><td>Module</td></tr>
       </table>
       >];
    2 -> 3;

    3 [fontname="courier", label=<
       <table border="0">
       <tr><td>ItemId(3)</td></tr>
       <tr><td>name</td><td>Quux</td></tr>
       <tr><td>kind</td><td>Type</td></tr>
       </table>
       >];
}

Which produces an image like this:

graph

C-assigned E-less-easy enhancement help wanted

Most helpful comment

Wow. It's might be interesting. I guess I'll take it, because I've never done something like this.

All 10 comments

@impowski, maybe you're interested in hacking on this?

For posterity, I used this command to go from a dot file to a png:

dot -Tpng graph.dot -o graph.png

This would be quite awesome actually, thanks for thinking about this @fitzgen!

Also this would be an easy way to diagnose which of the perf optimizations I mentioned in #59 would be more beneficial.

Looking at the dot crate's API, I suspect it might be easier to just write the output file directly without using the dot crate.

Wow. It's might be interesting. I guess I'll take it, because I've never done something like this.

If we didn't want to bring in an extra dependency, it shouldn't be hard to just write text to a file manually.

We could also feature-gate it, I guess.

Wow. It's might be interesting. I guess I'll take it, because I've never done something like this.

That's awesome! I'll mark it as assigned then :)

@impowski Great! Let me know if you need any more pointers or if you run into unexpected roadblocks :)

I very much look forward to having such a feature when debugging!! :-D

@emilio:

@fitzgen:

If we didn't want to bring in an extra dependency, it shouldn't be hard to just write text to a file manually.

We could also feature-gate it, I guess.

I think the dot crate's API just isn't a good fit for what we will be doing, so I don't think it is even worth that.

This was fixed in #508

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonhoo picture jonhoo  路  8Comments

bbigras picture bbigras  路  3Comments

jrmuizel picture jrmuizel  路  7Comments

maurer picture maurer  路  3Comments

DuckerMan picture DuckerMan  路  6Comments