]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/debug.rs
Rollup merge of #61698 - davidtwco:ice-const-generic-length, r=varkor
[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 {
21             text: text.trim().to_string()
22         }
23     }
24
25     /// Returns `true` if all nodes always pass the filter.
26     pub fn accepts_all(&self) -> bool {
27         self.text.is_empty()
28     }
29
30     /// Tests whether `node` meets the filter, returning true if so.
31     pub fn test(&self, node: &DepNode) -> bool {
32         let debug_str = format!("{:?}", node);
33         self.text.split('&')
34                  .map(|s| s.trim())
35                  .all(|f| debug_str.contains(f))
36     }
37 }
38
39 /// A filter like `F -> G` where `F` and `G` are valid dep-node
40 /// filters. This can be used to test the source/target independently.
41 pub struct EdgeFilter {
42     pub source: DepNodeFilter,
43     pub target: DepNodeFilter,
44 }
45
46 impl EdgeFilter {
47     pub fn new(test: &str) -> Result<EdgeFilter, Box<dyn Error>> {
48         let parts: Vec<_> = test.split("->").collect();
49         if parts.len() != 2 {
50             Err(format!("expected a filter like `a&b -> c&d`, not `{}`", test).into())
51         } else {
52             Ok(EdgeFilter {
53                 source: DepNodeFilter::new(parts[0]),
54                 target: DepNodeFilter::new(parts[1]),
55             })
56         }
57     }
58
59     pub fn test(&self,
60                 source: &DepNode,
61                 target: &DepNode)
62                 -> bool {
63         self.source.test(source) && self.target.test(target)
64     }
65 }