]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_system/src/dep_graph/debug.rs
Rollup merge of #106958 - jyn514:labels, r=m-ou-se
[rust.git] / compiler / rustc_query_system / src / dep_graph / debug.rs
1 //! Code for debugging the dep-graph.
2
3 use super::{DepKind, DepNode, DepNodeIndex};
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc_data_structures::sync::Lock;
6 use std::error::Error;
7
8 /// A dep-node filter goes from a user-defined string to a query over
9 /// nodes. Right now the format is like this:
10 /// ```ignore (illustrative)
11 /// x & y & z
12 /// ```
13 /// where the format-string of the dep-node must contain `x`, `y`, and
14 /// `z`.
15 #[derive(Debug)]
16 pub struct DepNodeFilter {
17     text: String,
18 }
19
20 impl DepNodeFilter {
21     pub fn new(text: &str) -> Self {
22         DepNodeFilter { text: text.trim().to_string() }
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<K: DepKind>(&self, node: &DepNode<K>) -> bool {
32         let debug_str = format!("{node:?}");
33         self.text.split('&').map(|s| s.trim()).all(|f| debug_str.contains(f))
34     }
35 }
36
37 /// A filter like `F -> G` where `F` and `G` are valid dep-node
38 /// filters. This can be used to test the source/target independently.
39 pub struct EdgeFilter<K: DepKind> {
40     pub source: DepNodeFilter,
41     pub target: DepNodeFilter,
42     pub index_to_node: Lock<FxHashMap<DepNodeIndex, DepNode<K>>>,
43 }
44
45 impl<K: DepKind> EdgeFilter<K> {
46     pub fn new(test: &str) -> Result<EdgeFilter<K>, Box<dyn Error>> {
47         let parts: Vec<_> = test.split("->").collect();
48         if parts.len() != 2 {
49             Err(format!("expected a filter like `a&b -> c&d`, not `{test}`").into())
50         } else {
51             Ok(EdgeFilter {
52                 source: DepNodeFilter::new(parts[0]),
53                 target: DepNodeFilter::new(parts[1]),
54                 index_to_node: Lock::new(FxHashMap::default()),
55             })
56         }
57     }
58
59     #[cfg(debug_assertions)]
60     pub fn test(&self, source: &DepNode<K>, target: &DepNode<K>) -> bool {
61         self.source.test(source) && self.target.test(target)
62     }
63 }