]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/debug.rs
Rollup merge of #68033 - ollie27:win_f32, r=dtolnay
[rust.git] / src / librustc / dep_graph / debug.rs
1 //! Code for debugging the dep-graph.
2
3 use super::dep_node::DepNode;
4 use std::error::Error;
5
6 /// A dep-node filter goes from a user-defined string to a query over
7 /// nodes. Right now the format is like this:
8 ///
9 ///     x & y & z
10 ///
11 /// where the format-string of the dep-node must contain `x`, `y`, and
12 /// `z`.
13 #[derive(Debug)]
14 pub struct DepNodeFilter {
15     text: String,
16 }
17
18 impl DepNodeFilter {
19     pub fn new(text: &str) -> Self {
20         DepNodeFilter { text: text.trim().to_string() }
21     }
22
23     /// Returns `true` if all nodes always pass the filter.
24     pub fn accepts_all(&self) -> bool {
25         self.text.is_empty()
26     }
27
28     /// Tests whether `node` meets the filter, returning true if so.
29     pub fn test(&self, node: &DepNode) -> bool {
30         let debug_str = format!("{:?}", node);
31         self.text.split('&').map(|s| s.trim()).all(|f| debug_str.contains(f))
32     }
33 }
34
35 /// A filter like `F -> G` where `F` and `G` are valid dep-node
36 /// filters. This can be used to test the source/target independently.
37 pub struct EdgeFilter {
38     pub source: DepNodeFilter,
39     pub target: DepNodeFilter,
40 }
41
42 impl EdgeFilter {
43     pub fn new(test: &str) -> Result<EdgeFilter, Box<dyn Error>> {
44         let parts: Vec<_> = test.split("->").collect();
45         if parts.len() != 2 {
46             Err(format!("expected a filter like `a&b -> c&d`, not `{}`", test).into())
47         } else {
48             Ok(EdgeFilter {
49                 source: DepNodeFilter::new(parts[0]),
50                 target: DepNodeFilter::new(parts[1]),
51             })
52         }
53     }
54
55     pub fn test(&self, source: &DepNode, target: &DepNode) -> bool {
56         self.source.test(source) && self.target.test(target)
57     }
58 }