2014-04-17 14:00:08 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
/// This module provides linkage between rustc::middle::graph and
|
|
|
|
/// libgraphviz traits.
|
|
|
|
|
2014-12-22 11:04:23 -06:00
|
|
|
use std::borrow::IntoCow;
|
|
|
|
|
|
|
|
// For clarity, rename the graphviz crate locally to dot.
|
2014-08-18 10:29:44 -05:00
|
|
|
use graphviz as dot;
|
2014-04-17 14:00:08 -05:00
|
|
|
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast_map;
|
|
|
|
|
|
|
|
use middle::cfg;
|
|
|
|
|
|
|
|
pub type Node<'a> = (cfg::CFGIndex, &'a cfg::CFGNode);
|
|
|
|
pub type Edge<'a> = &'a cfg::CFGEdge;
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
pub struct LabelledCFG<'a, 'ast: 'a> {
|
|
|
|
pub ast_map: &'a ast_map::Map<'ast>,
|
2014-04-17 14:00:08 -05:00
|
|
|
pub cfg: &'a cfg::CFG,
|
2014-05-22 18:57:53 -05:00
|
|
|
pub name: String,
|
2015-01-12 15:42:12 -06:00
|
|
|
/// `labelled_edges` controls whether we emit labels on the edges
|
|
|
|
pub labelled_edges: bool,
|
2014-04-17 14:00:08 -05:00
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
fn replace_newline_with_backslash_l(s: String) -> String {
|
2014-04-17 14:00:08 -05:00
|
|
|
// Replacing newlines with \\l causes each line to be left-aligned,
|
|
|
|
// improving presentation of (long) pretty-printed expressions.
|
2014-11-27 12:53:34 -06:00
|
|
|
if s.contains("\n") {
|
2014-04-17 14:00:08 -05:00
|
|
|
let mut s = s.replace("\n", "\\l");
|
|
|
|
// Apparently left-alignment applies to the line that precedes
|
|
|
|
// \l, not the line that follows; so, add \l at end of string
|
|
|
|
// if not already present, ensuring last line gets left-aligned
|
|
|
|
// as well.
|
2014-05-19 19:23:26 -05:00
|
|
|
let mut last_two: Vec<_> =
|
2014-11-27 12:53:34 -06:00
|
|
|
s.chars().rev().take(2).collect();
|
2014-04-17 14:00:08 -05:00
|
|
|
last_two.reverse();
|
2014-11-21 00:20:04 -06:00
|
|
|
if last_two != ['\\', 'l'] {
|
2014-10-15 01:05:01 -05:00
|
|
|
s.push_str("\\l");
|
2014-04-17 14:00:08 -05:00
|
|
|
}
|
2014-10-15 01:05:01 -05:00
|
|
|
s
|
2014-04-17 14:00:08 -05:00
|
|
|
} else {
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> {
|
2015-02-20 13:08:14 -06:00
|
|
|
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).ok().unwrap() }
|
2014-04-17 14:00:08 -05:00
|
|
|
|
|
|
|
fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
|
std: Stabilize the std::fmt module
This commit performs a final stabilization pass over the std::fmt module,
marking all necessary APIs as stable. One of the more interesting aspects of
this module is that it exposes a good deal of its runtime representation to the
outside world in order for `format_args!` to be able to construct the format
strings. Instead of hacking the compiler to assume that these items are stable,
this commit instead lays out a story for the stabilization and evolution of
these APIs.
There are three primary details used by the `format_args!` macro:
1. `Arguments` - an opaque package of a "compiled format string". This structure
is passed around and the `write` function is the source of truth for
transforming a compiled format string into a string at runtime. This must be
able to be constructed in stable code.
2. `Argument` - an opaque structure representing an argument to a format string.
This is *almost* a trait object as it's just a pointer/function pair, but due
to the function originating from one of many traits, it's not actually a
trait object. Like `Arguments`, this must be constructed from stable code.
3. `fmt::rt` - this module contains the runtime type definitions primarily for
the `rt::Argument` structure. Whenever an argument is formatted with
nonstandard flags, a corresponding `rt::Argument` is generated describing how
the argument is being formatted. This can be used to construct an
`Arguments`.
The primary interface to `std::fmt` is the `Arguments` structure, and as such
this type name is stabilize as-is today. It is expected for libraries to pass
around an `Arguments` structure to represent a pending formatted computation.
The remaining portions are largely "cruft" which would rather not be stabilized,
but due to the stability checks they must be. As a result, almost all pieces
have been renamed to represent that they are "version 1" of the formatting
representation. The theory is that at a later date if we change the
representation of these types we can add new definitions called "version 2" and
corresponding constructors for `Arguments`.
One of the other remaining large questions about the fmt module were how the
pending I/O reform would affect the signatures of methods in the module. Due to
[RFC 526][rfc], however, the writers of fmt are now incompatible with the
writers of io, so this question has largely been solved. As a result the
interfaces are largely stabilized as-is today.
[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0526-fmt-text-writer.md
Specifically, the following changes were made:
* The contents of `fmt::rt` were all moved under `fmt::rt::v1`
* `fmt::rt` is stable
* `fmt::rt::v1` is stable
* `Error` is stable
* `Writer` is stable
* `Writer::write_str` is stable
* `Writer::write_fmt` is stable
* `Formatter` is stable
* `Argument` has been renamed to `ArgumentV1` and is stable
* `ArgumentV1::new` is stable
* `ArgumentV1::from_uint` is stable
* `Arguments::new_v1` is stable (renamed from `new`)
* `Arguments::new_v1_formatted` is stable (renamed from `with_placeholders`)
* All formatting traits are now stable, as well as the `fmt` method.
* `fmt::write` is stable
* `fmt::format` is stable
* `Formatter::pad_integral` is stable
* `Formatter::pad` is stable
* `Formatter::write_str` is stable
* `Formatter::write_fmt` is stable
* Some assorted top level items which were only used by `format_args!` were
removed in favor of static functions on `ArgumentV1` as well.
* The formatting-flag-accessing methods remain unstable
Within the contents of the `fmt::rt::v1` module, the following actions were
taken:
* Reexports of all enum variants were removed
* All prefixes on enum variants were removed
* A few miscellaneous enum variants were renamed
* Otherwise all structs, fields, and variants were marked stable.
In addition to these actions in the `std::fmt` module, many implementations of
`Show` and `String` were stabilized as well.
In some other modules:
* `ToString` is now stable
* `ToString::to_string` is now stable
* `Vec` no longer implements `fmt::Writer` (this has moved to `String`)
This is a breaking change due to all of the changes to the `fmt::rt` module, but
this likely will not have much impact on existing programs.
Closes #20661
[breaking-change]
2015-01-13 17:42:53 -06:00
|
|
|
dot::Id::new(format!("N{}", i.node_id())).ok().unwrap()
|
2014-04-17 14:00:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn node_label(&'a self, &(i, n): &Node<'a>) -> dot::LabelText<'a> {
|
|
|
|
if i == self.cfg.entry {
|
2015-01-02 21:31:50 -06:00
|
|
|
dot::LabelText::LabelStr("entry".into_cow())
|
2014-04-17 14:00:08 -05:00
|
|
|
} else if i == self.cfg.exit {
|
2015-01-02 21:31:50 -06:00
|
|
|
dot::LabelText::LabelStr("exit".into_cow())
|
2014-04-17 14:00:08 -05:00
|
|
|
} else if n.data.id == ast::DUMMY_NODE_ID {
|
2015-01-02 21:31:50 -06:00
|
|
|
dot::LabelText::LabelStr("(dummy_node)".into_cow())
|
2014-04-17 14:00:08 -05:00
|
|
|
} else {
|
2014-06-21 05:39:03 -05:00
|
|
|
let s = self.ast_map.node_to_string(n.data.id);
|
2014-04-17 14:00:08 -05:00
|
|
|
// left-aligns the lines
|
|
|
|
let s = replace_newline_with_backslash_l(s);
|
2015-01-02 21:31:50 -06:00
|
|
|
dot::LabelText::EscStr(s.into_cow())
|
2014-04-17 14:00:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn edge_label(&self, e: &Edge<'a>) -> dot::LabelText<'a> {
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut label = String::new();
|
2015-01-12 15:42:12 -06:00
|
|
|
if !self.labelled_edges {
|
|
|
|
return dot::LabelText::EscStr(label.into_cow());
|
|
|
|
}
|
2014-04-17 14:00:08 -05:00
|
|
|
let mut put_one = false;
|
|
|
|
for (i, &node_id) in e.data.exiting_scopes.iter().enumerate() {
|
|
|
|
if put_one {
|
2014-10-15 01:05:01 -05:00
|
|
|
label.push_str(",\\l");
|
2014-04-17 14:00:08 -05:00
|
|
|
} else {
|
|
|
|
put_one = true;
|
|
|
|
}
|
2014-06-21 05:39:03 -05:00
|
|
|
let s = self.ast_map.node_to_string(node_id);
|
2014-04-17 14:00:08 -05:00
|
|
|
// left-aligns the lines
|
|
|
|
let s = replace_newline_with_backslash_l(s);
|
2015-01-07 10:58:31 -06:00
|
|
|
label.push_str(&format!("exiting scope_{} {}",
|
2015-01-03 22:43:24 -06:00
|
|
|
i,
|
2015-02-20 13:08:14 -06:00
|
|
|
&s[..]));
|
2014-04-17 14:00:08 -05:00
|
|
|
}
|
2015-01-02 21:31:50 -06:00
|
|
|
dot::LabelText::EscStr(label.into_cow())
|
2014-04-17 14:00:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for &'a cfg::CFG {
|
2014-10-17 08:11:10 -05:00
|
|
|
fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> {
|
2014-04-17 14:00:08 -05:00
|
|
|
let mut v = Vec::new();
|
|
|
|
self.graph.each_node(|i, nd| { v.push((i, nd)); true });
|
2014-11-21 16:10:42 -06:00
|
|
|
v.into_cow()
|
2014-04-17 14:00:08 -05:00
|
|
|
}
|
2014-10-17 08:11:10 -05:00
|
|
|
fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
|
2014-04-17 14:00:08 -05:00
|
|
|
self.graph.all_edges().iter().collect()
|
|
|
|
}
|
2014-10-17 08:11:10 -05:00
|
|
|
fn source(&'a self, edge: &Edge<'a>) -> Node<'a> {
|
2014-04-17 14:00:08 -05:00
|
|
|
let i = edge.source();
|
|
|
|
(i, self.graph.node(i))
|
|
|
|
}
|
2014-10-17 08:11:10 -05:00
|
|
|
fn target(&'a self, edge: &Edge<'a>) -> Node<'a> {
|
2014-04-17 14:00:08 -05:00
|
|
|
let i = edge.target();
|
|
|
|
(i, self.graph.node(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
impl<'a, 'ast> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast>
|
2014-04-17 14:00:08 -05:00
|
|
|
{
|
2014-10-17 08:11:10 -05:00
|
|
|
fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> { self.cfg.nodes() }
|
|
|
|
fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { self.cfg.edges() }
|
|
|
|
fn source(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.source(edge) }
|
|
|
|
fn target(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.target(edge) }
|
2014-04-17 14:00:08 -05:00
|
|
|
}
|
2014-07-02 10:50:18 -05:00
|
|
|
|