]>
Commit | Line | Data |
---|---|---|
a7813a04 XL |
1 | //! Code for debugging the dep-graph. |
2 | ||
cdc7bbd5 XL |
3 | use super::{DepKind, DepNode, DepNodeIndex}; |
4 | use rustc_data_structures::fx::FxHashMap; | |
5 | use rustc_data_structures::sync::Lock; | |
a7813a04 | 6 | use std::error::Error; |
a7813a04 XL |
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: | |
04454e1e FG |
10 | /// ```ignore (illustrative) |
11 | /// x & y & z | |
12 | /// ``` | |
a7813a04 XL |
13 | /// where the format-string of the dep-node must contain `x`, `y`, and |
14 | /// `z`. | |
15 | #[derive(Debug)] | |
16 | pub struct DepNodeFilter { | |
dfeec247 | 17 | text: String, |
a7813a04 XL |
18 | } |
19 | ||
20 | impl DepNodeFilter { | |
21 | pub fn new(text: &str) -> Self { | |
dfeec247 | 22 | DepNodeFilter { text: text.trim().to_string() } |
a7813a04 XL |
23 | } |
24 | ||
9fa01778 | 25 | /// Returns `true` if all nodes always pass the filter. |
a7813a04 XL |
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. | |
ba9703b0 | 31 | pub fn test<K: DepKind>(&self, node: &DepNode<K>) -> bool { |
9c376795 | 32 | let debug_str = format!("{node:?}"); |
dfeec247 | 33 | self.text.split('&').map(|s| s.trim()).all(|f| debug_str.contains(f)) |
a7813a04 XL |
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. | |
cdc7bbd5 | 39 | pub struct EdgeFilter<K: DepKind> { |
a7813a04 XL |
40 | pub source: DepNodeFilter, |
41 | pub target: DepNodeFilter, | |
cdc7bbd5 | 42 | pub index_to_node: Lock<FxHashMap<DepNodeIndex, DepNode<K>>>, |
a7813a04 XL |
43 | } |
44 | ||
cdc7bbd5 XL |
45 | impl<K: DepKind> EdgeFilter<K> { |
46 | pub fn new(test: &str) -> Result<EdgeFilter<K>, Box<dyn Error>> { | |
a7813a04 XL |
47 | let parts: Vec<_> = test.split("->").collect(); |
48 | if parts.len() != 2 { | |
9c376795 | 49 | Err(format!("expected a filter like `a&b -> c&d`, not `{test}`").into()) |
a7813a04 XL |
50 | } else { |
51 | Ok(EdgeFilter { | |
52 | source: DepNodeFilter::new(parts[0]), | |
53 | target: DepNodeFilter::new(parts[1]), | |
cdc7bbd5 | 54 | index_to_node: Lock::new(FxHashMap::default()), |
a7813a04 XL |
55 | }) |
56 | } | |
57 | } | |
9e0c209e | 58 | |
cdc7bbd5 XL |
59 | #[cfg(debug_assertions)] |
60 | pub fn test(&self, source: &DepNode<K>, target: &DepNode<K>) -> bool { | |
9e0c209e SL |
61 | self.source.test(source) && self.target.test(target) |
62 | } | |
a7813a04 | 63 | } |