2015-12-22 16:17:27 -06:00
|
|
|
//! This pass is only used for the UNIT TESTS and DEBUGGING NEEDS
|
|
|
|
//! around dependency graph construction. It serves two purposes; it
|
|
|
|
//! will dump graphs in graphviz form to disk, and it searches for
|
|
|
|
//! `#[rustc_if_this_changed]` and `#[rustc_then_this_would_need]`
|
|
|
|
//! annotations. These annotations can be used to test whether paths
|
2018-05-08 08:10:16 -05:00
|
|
|
//! exist in the graph. These checks run after codegen, so they view the
|
2016-03-28 16:36:56 -05:00
|
|
|
//! the final state of the dependency graph. Note that there are
|
|
|
|
//! similar assertions found in `persist::dirty_clean` which check the
|
|
|
|
//! **initial** state of the dependency graph, just after it has been
|
|
|
|
//! loaded from disk.
|
|
|
|
//!
|
|
|
|
//! In this code, we report errors on each `rustc_if_this_changed`
|
|
|
|
//! annotation. If a path exists in all cases, then we would report
|
|
|
|
//! "all path(s) exist". Otherwise, we report: "no path to `foo`" for
|
2020-12-28 11:15:16 -06:00
|
|
|
//! each case where no path exists. `ui` tests can then be
|
2016-03-28 16:36:56 -05:00
|
|
|
//! used to check when paths exist or do not.
|
2015-12-22 16:17:27 -06:00
|
|
|
//!
|
|
|
|
//! The full form of the `rustc_if_this_changed` annotation is
|
2016-08-24 10:00:55 -05:00
|
|
|
//! `#[rustc_if_this_changed("foo")]`, which will report a
|
|
|
|
//! source node of `foo(def_id)`. The `"foo"` is optional and
|
|
|
|
//! defaults to `"Hir"` if omitted.
|
2015-12-22 16:17:27 -06:00
|
|
|
//!
|
|
|
|
//! Example:
|
|
|
|
//!
|
2022-04-15 17:04:34 -05:00
|
|
|
//! ```ignore (needs flags)
|
2016-08-24 10:00:55 -05:00
|
|
|
//! #[rustc_if_this_changed(Hir)]
|
2015-12-22 16:17:27 -06:00
|
|
|
//! fn foo() { }
|
|
|
|
//!
|
2018-05-08 08:10:16 -05:00
|
|
|
//! #[rustc_then_this_would_need(codegen)] //~ ERROR no path from `foo`
|
2015-12-22 16:17:27 -06:00
|
|
|
//! fn bar() { }
|
|
|
|
//!
|
2018-05-08 08:10:16 -05:00
|
|
|
//! #[rustc_then_this_would_need(codegen)] //~ ERROR OK
|
2015-12-22 16:17:27 -06:00
|
|
|
//! fn baz() { foo(); }
|
|
|
|
//! ```
|
|
|
|
|
2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast as ast;
|
2016-11-07 21:02:55 -06:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2018-07-02 05:14:49 -05:00
|
|
|
use rustc_data_structures::graph::implementation::{Direction, NodeIndex, INCOMING, OUTGOING};
|
2020-06-02 12:19:49 -05:00
|
|
|
use rustc_graphviz as dot;
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::DefId;
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_hir::intravisit::{self, Visitor};
|
2021-03-02 15:38:49 -06:00
|
|
|
use rustc_middle::dep_graph::{
|
|
|
|
DepGraphQuery, DepKind, DepNode, DepNodeExt, DepNodeFilter, EdgeFilter,
|
|
|
|
};
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-04-19 06:00:18 -05:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::Span;
|
2020-01-01 12:30:57 -06:00
|
|
|
|
2015-12-22 16:17:27 -06:00
|
|
|
use std::env;
|
2018-01-10 10:58:39 -06:00
|
|
|
use std::fs::{self, File};
|
2020-01-22 09:22:46 -06:00
|
|
|
use std::io::{BufWriter, Write};
|
2015-12-22 16:17:27 -06:00
|
|
|
|
2021-10-29 12:14:17 -05:00
|
|
|
#[allow(missing_docs)]
|
2019-06-21 13:27:44 -05:00
|
|
|
pub fn assert_dep_graph(tcx: TyCtxt<'_>) {
|
2017-12-27 23:05:45 -06:00
|
|
|
tcx.dep_graph.with_ignore(|| {
|
2022-07-06 07:44:47 -05:00
|
|
|
if tcx.sess.opts.unstable_opts.dump_dep_graph {
|
2021-03-02 15:38:49 -06:00
|
|
|
tcx.dep_graph.with_query(dump_graph);
|
2017-12-27 23:05:45 -06:00
|
|
|
}
|
2015-12-22 16:17:27 -06:00
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if !tcx.sess.opts.unstable_opts.query_dep_graph {
|
2021-03-19 20:14:04 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-27 23:05:45 -06:00
|
|
|
// if the `rustc_attrs` feature is not enabled, then the
|
|
|
|
// attributes we are interested in cannot be present anyway, so
|
|
|
|
// skip the walk.
|
2018-02-14 09:11:02 -06:00
|
|
|
if !tcx.features().rustc_attrs {
|
2017-12-27 23:05:45 -06:00
|
|
|
return;
|
|
|
|
}
|
2016-08-03 18:33:51 -05:00
|
|
|
|
2017-12-27 23:05:45 -06:00
|
|
|
// Find annotations supplied by user (if any).
|
|
|
|
let (if_this_changed, then_this_would_need) = {
|
|
|
|
let mut visitor =
|
|
|
|
IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
|
2020-11-26 16:23:01 -06:00
|
|
|
visitor.process_attrs(hir::CRATE_HIR_ID);
|
2022-07-03 08:28:57 -05:00
|
|
|
tcx.hir().visit_all_item_likes_in_crate(&mut visitor);
|
2017-12-27 23:05:45 -06:00
|
|
|
(visitor.if_this_changed, visitor.then_this_would_need)
|
|
|
|
};
|
2015-12-22 16:17:27 -06:00
|
|
|
|
2017-12-27 23:05:45 -06:00
|
|
|
if !if_this_changed.is_empty() || !then_this_would_need.is_empty() {
|
|
|
|
assert!(
|
2022-07-06 07:44:47 -05:00
|
|
|
tcx.sess.opts.unstable_opts.query_dep_graph,
|
2017-12-27 23:05:45 -06:00
|
|
|
"cannot use the `#[{}]` or `#[{}]` annotations \
|
|
|
|
without supplying `-Z query-dep-graph`",
|
2019-10-27 22:02:59 -05:00
|
|
|
sym::rustc_if_this_changed,
|
|
|
|
sym::rustc_then_this_would_need
|
|
|
|
);
|
2017-12-27 23:05:45 -06:00
|
|
|
}
|
2016-03-28 16:36:56 -05:00
|
|
|
|
2017-12-27 23:05:45 -06:00
|
|
|
// Check paths.
|
|
|
|
check_paths(tcx, &if_this_changed, &then_this_would_need);
|
|
|
|
})
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:36:30 -05:00
|
|
|
type Sources = Vec<(Span, DefId, DepNode)>;
|
2020-04-19 06:00:18 -05:00
|
|
|
type Targets = Vec<(Span, Symbol, hir::HirId, DepNode)>;
|
2015-12-22 16:17:27 -06:00
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
struct IfThisChanged<'tcx> {
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2016-08-24 10:00:55 -05:00
|
|
|
if_this_changed: Sources,
|
|
|
|
then_this_would_need: Targets,
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
|
2021-12-13 22:49:21 -06:00
|
|
|
impl<'tcx> IfThisChanged<'tcx> {
|
2020-04-19 06:00:18 -05:00
|
|
|
fn argument(&self, attr: &ast::Attribute) -> Option<Symbol> {
|
2016-08-24 10:00:55 -05:00
|
|
|
let mut value = None;
|
|
|
|
for list_item in attr.meta_item_list().unwrap_or_default() {
|
2019-02-28 00:17:24 -06:00
|
|
|
match list_item.ident() {
|
|
|
|
Some(ident) if list_item.is_word() && value.is_none() => value = Some(ident.name),
|
2016-08-24 10:00:55 -05:00
|
|
|
_ =>
|
|
|
|
// FIXME better-encapsulate meta_item (don't directly access `node`)
|
2019-12-22 16:42:04 -06:00
|
|
|
{
|
2019-03-03 11:56:24 -06:00
|
|
|
span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item)
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2016-08-24 10:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:23:01 -06:00
|
|
|
fn process_attrs(&mut self, hir_id: hir::HirId) {
|
2019-06-27 04:28:14 -05:00
|
|
|
let def_id = self.tcx.hir().local_def_id(hir_id);
|
2020-04-09 03:43:00 -05:00
|
|
|
let def_path_hash = self.tcx.def_path_hash(def_id.to_def_id());
|
2020-11-26 16:23:01 -06:00
|
|
|
let attrs = self.tcx.hir().attrs(hir_id);
|
2016-08-24 10:00:55 -05:00
|
|
|
for attr in attrs {
|
2021-07-29 12:00:41 -05:00
|
|
|
if attr.has_name(sym::rustc_if_this_changed) {
|
2016-08-24 10:00:55 -05:00
|
|
|
let dep_node_interned = self.argument(attr);
|
|
|
|
let dep_node = match dep_node_interned {
|
2021-10-16 13:10:23 -05:00
|
|
|
None => {
|
|
|
|
DepNode::from_def_path_hash(self.tcx, def_path_hash, DepKind::hir_owner)
|
|
|
|
}
|
|
|
|
Some(n) => {
|
2021-12-14 21:39:23 -06:00
|
|
|
match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {
|
2021-10-16 13:10:23 -05:00
|
|
|
Ok(n) => n,
|
|
|
|
Err(()) => {
|
|
|
|
self.tcx.sess.span_fatal(
|
|
|
|
attr.span,
|
|
|
|
&format!("unrecognized DepNode variant {:?}", n),
|
|
|
|
);
|
|
|
|
}
|
2016-08-19 20:58:14 -05:00
|
|
|
}
|
2021-10-16 13:10:23 -05:00
|
|
|
}
|
2016-08-24 10:00:55 -05:00
|
|
|
};
|
2020-04-09 03:43:00 -05:00
|
|
|
self.if_this_changed.push((attr.span, def_id.to_def_id(), dep_node));
|
2021-07-29 12:00:41 -05:00
|
|
|
} else if attr.has_name(sym::rustc_then_this_would_need) {
|
2016-08-24 10:00:55 -05:00
|
|
|
let dep_node_interned = self.argument(attr);
|
2016-03-28 16:36:56 -05:00
|
|
|
let dep_node = match dep_node_interned {
|
2021-10-16 13:10:23 -05:00
|
|
|
Some(n) => {
|
2021-12-14 21:39:23 -06:00
|
|
|
match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {
|
2021-10-16 13:10:23 -05:00
|
|
|
Ok(n) => n,
|
|
|
|
Err(()) => {
|
|
|
|
self.tcx.sess.span_fatal(
|
|
|
|
attr.span,
|
|
|
|
&format!("unrecognized DepNode variant {:?}", n),
|
|
|
|
);
|
|
|
|
}
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
2021-10-16 13:10:23 -05:00
|
|
|
}
|
2016-03-28 16:36:56 -05:00
|
|
|
None => {
|
2017-05-04 07:17:23 -05:00
|
|
|
self.tcx.sess.span_fatal(attr.span, "missing DepNode variant");
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
};
|
2016-08-24 10:00:55 -05:00
|
|
|
self.then_this_would_need.push((
|
|
|
|
attr.span,
|
2016-11-14 22:34:52 -06:00
|
|
|
dep_node_interned.unwrap(),
|
2019-02-26 04:04:58 -06:00
|
|
|
hir_id,
|
2016-08-24 10:00:55 -05:00
|
|
|
dep_node,
|
|
|
|
));
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 22:49:21 -06:00
|
|
|
impl<'tcx> Visitor<'tcx> for IfThisChanged<'tcx> {
|
2021-11-03 18:03:12 -05:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2020-01-07 10:25:33 -06:00
|
|
|
|
2021-11-03 18:03:12 -05:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.tcx.hir()
|
2017-04-25 08:08:21 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 12:28:50 -06:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
|
2020-11-26 16:23:01 -06:00
|
|
|
self.process_attrs(item.hir_id());
|
2017-04-25 08:08:21 -05:00
|
|
|
intravisit::walk_item(self, item);
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
2016-11-04 17:20:15 -05:00
|
|
|
|
2019-11-28 14:47:10 -06:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
|
2020-11-26 16:23:01 -06:00
|
|
|
self.process_attrs(trait_item.hir_id());
|
2017-04-25 08:08:21 -05:00
|
|
|
intravisit::walk_trait_item(self, trait_item);
|
2016-12-03 20:21:06 -06:00
|
|
|
}
|
|
|
|
|
2019-11-28 15:16:44 -06:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
|
2020-11-26 16:23:01 -06:00
|
|
|
self.process_attrs(impl_item.hir_id());
|
2017-04-25 08:08:21 -05:00
|
|
|
intravisit::walk_impl_item(self, impl_item);
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
|
2020-11-26 16:23:01 -06:00
|
|
|
self.process_attrs(s.hir_id);
|
2021-03-15 16:36:07 -05:00
|
|
|
intravisit::walk_field_def(self, s);
|
2016-11-04 17:20:15 -05:00
|
|
|
}
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
|
2019-06-13 17:32:15 -05:00
|
|
|
fn check_paths<'tcx>(tcx: TyCtxt<'tcx>, if_this_changed: &Sources, then_this_would_need: &Targets) {
|
2015-12-22 16:17:27 -06:00
|
|
|
// Return early here so as not to construct the query, which is not cheap.
|
|
|
|
if if_this_changed.is_empty() {
|
2016-08-24 10:00:55 -05:00
|
|
|
for &(target_span, _, _, _) in then_this_would_need {
|
2019-07-20 11:49:34 -05:00
|
|
|
tcx.sess.span_err(target_span, "no `#[rustc_if_this_changed]` annotation detected");
|
2016-08-24 10:00:55 -05:00
|
|
|
}
|
2015-12-22 16:17:27 -06:00
|
|
|
return;
|
|
|
|
}
|
2021-03-02 15:38:49 -06:00
|
|
|
tcx.dep_graph.with_query(|query| {
|
|
|
|
for &(_, source_def_id, ref source_dep_node) in if_this_changed {
|
|
|
|
let dependents = query.transitive_predecessors(source_dep_node);
|
|
|
|
for &(target_span, ref target_pass, _, ref target_dep_node) in then_this_would_need {
|
|
|
|
if !dependents.contains(&target_dep_node) {
|
|
|
|
tcx.sess.span_err(
|
|
|
|
target_span,
|
|
|
|
&format!(
|
|
|
|
"no path from `{}` to `{}`",
|
|
|
|
tcx.def_path_str(source_def_id),
|
|
|
|
target_pass
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
tcx.sess.span_err(target_span, "OK");
|
|
|
|
}
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
}
|
2021-03-02 15:38:49 -06:00
|
|
|
});
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
|
2021-03-02 15:38:49 -06:00
|
|
|
fn dump_graph(query: &DepGraphQuery) {
|
2018-07-28 07:40:32 -05:00
|
|
|
let path: String = env::var("RUST_DEP_GRAPH").unwrap_or_else(|_| "dep_graph".to_string());
|
2015-12-22 16:17:27 -06:00
|
|
|
|
|
|
|
let nodes = match env::var("RUST_DEP_GRAPH_FILTER") {
|
|
|
|
Ok(string) => {
|
|
|
|
// Expect one of: "-> target", "source -> target", or "source ->".
|
2016-05-06 04:02:05 -05:00
|
|
|
let edge_filter =
|
|
|
|
EdgeFilter::new(&string).unwrap_or_else(|e| bug!("invalid filter: {}", e));
|
|
|
|
let sources = node_set(&query, &edge_filter.source);
|
|
|
|
let targets = node_set(&query, &edge_filter.target);
|
2015-12-22 16:17:27 -06:00
|
|
|
filter_nodes(&query, &sources, &targets)
|
|
|
|
}
|
2022-03-28 08:59:37 -05:00
|
|
|
Err(_) => query.nodes().into_iter().map(|n| n.kind).collect(),
|
2015-12-22 16:17:27 -06:00
|
|
|
};
|
|
|
|
let edges = filter_edges(&query, &nodes);
|
|
|
|
|
|
|
|
{
|
|
|
|
// dump a .txt file with just the edges:
|
|
|
|
let txt_path = format!("{}.txt", path);
|
2020-01-22 09:22:46 -06:00
|
|
|
let mut file = BufWriter::new(File::create(&txt_path).unwrap());
|
2016-05-26 05:11:16 -05:00
|
|
|
for &(ref source, ref target) in &edges {
|
2015-12-22 16:17:27 -06:00
|
|
|
write!(file, "{:?} -> {:?}\n", source, target).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// dump a .dot file in graphviz format:
|
|
|
|
let dot_path = format!("{}.dot", path);
|
|
|
|
let mut v = Vec::new();
|
|
|
|
dot::render(&GraphvizDepGraph(nodes, edges), &mut v).unwrap();
|
2018-01-10 10:58:39 -06:00
|
|
|
fs::write(dot_path, v).unwrap();
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 12:14:17 -05:00
|
|
|
#[allow(missing_docs)]
|
2022-03-28 08:59:37 -05:00
|
|
|
pub struct GraphvizDepGraph(FxHashSet<DepKind>, Vec<(DepKind, DepKind)>);
|
2015-12-22 16:17:27 -06:00
|
|
|
|
2022-03-28 08:59:37 -05:00
|
|
|
impl<'a> dot::GraphWalk<'a> for GraphvizDepGraph {
|
|
|
|
type Node = DepKind;
|
|
|
|
type Edge = (DepKind, DepKind);
|
|
|
|
fn nodes(&self) -> dot::Nodes<'_, DepKind> {
|
2015-12-22 16:17:27 -06:00
|
|
|
let nodes: Vec<_> = self.0.iter().cloned().collect();
|
2018-10-05 07:16:10 -05:00
|
|
|
nodes.into()
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
2022-03-28 08:59:37 -05:00
|
|
|
fn edges(&self) -> dot::Edges<'_, (DepKind, DepKind)> {
|
2018-10-05 07:16:10 -05:00
|
|
|
self.1[..].into()
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
2022-03-28 08:59:37 -05:00
|
|
|
fn source(&self, edge: &(DepKind, DepKind)) -> DepKind {
|
2015-12-22 16:17:27 -06:00
|
|
|
edge.0
|
|
|
|
}
|
2022-03-28 08:59:37 -05:00
|
|
|
fn target(&self, edge: &(DepKind, DepKind)) -> DepKind {
|
2015-12-22 16:17:27 -06:00
|
|
|
edge.1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:59:37 -05:00
|
|
|
impl<'a> dot::Labeller<'a> for GraphvizDepGraph {
|
|
|
|
type Node = DepKind;
|
|
|
|
type Edge = (DepKind, DepKind);
|
2019-02-08 06:16:35 -06:00
|
|
|
fn graph_id(&self) -> dot::Id<'_> {
|
2015-12-22 16:17:27 -06:00
|
|
|
dot::Id::new("DependencyGraph").unwrap()
|
|
|
|
}
|
2022-03-28 08:59:37 -05:00
|
|
|
fn node_id(&self, n: &DepKind) -> dot::Id<'_> {
|
2015-12-22 16:17:27 -06:00
|
|
|
let s: String = format!("{:?}", n)
|
|
|
|
.chars()
|
|
|
|
.map(|c| if c == '_' || c.is_alphanumeric() { c } else { '_' })
|
|
|
|
.collect();
|
|
|
|
debug!("n={:?} s={:?}", n, s);
|
|
|
|
dot::Id::new(s).unwrap()
|
|
|
|
}
|
2022-03-28 08:59:37 -05:00
|
|
|
fn node_label(&self, n: &DepKind) -> dot::LabelText<'_> {
|
2015-12-22 16:17:27 -06:00
|
|
|
dot::LabelText::label(format!("{:?}", n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given an optional filter like `"x,y,z"`, returns either `None` (no
|
|
|
|
// filter) or the set of nodes whose labels contain all of those
|
|
|
|
// substrings.
|
2017-06-02 10:36:30 -05:00
|
|
|
fn node_set<'q>(
|
|
|
|
query: &'q DepGraphQuery,
|
|
|
|
filter: &DepNodeFilter,
|
|
|
|
) -> Option<FxHashSet<&'q DepNode>> {
|
2015-12-22 16:17:27 -06:00
|
|
|
debug!("node_set(filter={:?})", filter);
|
|
|
|
|
2016-05-06 04:02:05 -05:00
|
|
|
if filter.accepts_all() {
|
2015-12-22 16:17:27 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:02:05 -05:00
|
|
|
Some(query.nodes().into_iter().filter(|n| filter.test(n)).collect())
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:36:30 -05:00
|
|
|
fn filter_nodes<'q>(
|
|
|
|
query: &'q DepGraphQuery,
|
|
|
|
sources: &Option<FxHashSet<&'q DepNode>>,
|
|
|
|
targets: &Option<FxHashSet<&'q DepNode>>,
|
2022-03-28 08:59:37 -05:00
|
|
|
) -> FxHashSet<DepKind> {
|
2021-01-02 13:09:17 -06:00
|
|
|
if let Some(sources) = sources {
|
|
|
|
if let Some(targets) = targets {
|
2015-12-22 16:17:27 -06:00
|
|
|
walk_between(query, sources, targets)
|
|
|
|
} else {
|
|
|
|
walk_nodes(query, sources, OUTGOING)
|
|
|
|
}
|
2021-01-02 13:09:17 -06:00
|
|
|
} else if let Some(targets) = targets {
|
2015-12-22 16:17:27 -06:00
|
|
|
walk_nodes(query, targets, INCOMING)
|
|
|
|
} else {
|
2022-03-28 08:59:37 -05:00
|
|
|
query.nodes().into_iter().map(|n| n.kind).collect()
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:36:30 -05:00
|
|
|
fn walk_nodes<'q>(
|
|
|
|
query: &'q DepGraphQuery,
|
|
|
|
starts: &FxHashSet<&'q DepNode>,
|
2016-05-26 05:11:16 -05:00
|
|
|
direction: Direction,
|
2022-03-28 08:59:37 -05:00
|
|
|
) -> FxHashSet<DepKind> {
|
2018-10-16 03:44:26 -05:00
|
|
|
let mut set = FxHashSet::default();
|
2016-05-26 05:11:16 -05:00
|
|
|
for &start in starts {
|
2015-12-22 16:17:27 -06:00
|
|
|
debug!("walk_nodes: start={:?} outgoing?={:?}", start, direction == OUTGOING);
|
2022-03-28 08:59:37 -05:00
|
|
|
if set.insert(start.kind) {
|
2015-12-22 16:17:27 -06:00
|
|
|
let mut stack = vec![query.indices[start]];
|
|
|
|
while let Some(index) = stack.pop() {
|
|
|
|
for (_, edge) in query.graph.adjacent_edges(index, direction) {
|
|
|
|
let neighbor_index = edge.source_or_target(direction);
|
|
|
|
let neighbor = query.graph.node_data(neighbor_index);
|
2022-03-28 08:59:37 -05:00
|
|
|
if set.insert(neighbor.kind) {
|
2015-12-22 16:17:27 -06:00
|
|
|
stack.push(neighbor_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:36:30 -05:00
|
|
|
fn walk_between<'q>(
|
|
|
|
query: &'q DepGraphQuery,
|
|
|
|
sources: &FxHashSet<&'q DepNode>,
|
|
|
|
targets: &FxHashSet<&'q DepNode>,
|
2022-03-28 08:59:37 -05:00
|
|
|
) -> FxHashSet<DepKind> {
|
2015-12-22 16:17:27 -06:00
|
|
|
// This is a bit tricky. We want to include a node only if it is:
|
|
|
|
// (a) reachable from a source and (b) will reach a target. And we
|
|
|
|
// have to be careful about cycles etc. Luckily efficiency is not
|
|
|
|
// a big concern!
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
enum State {
|
|
|
|
Undecided,
|
|
|
|
Deciding,
|
|
|
|
Included,
|
|
|
|
Excluded,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut node_states = vec![State::Undecided; query.graph.len_nodes()];
|
|
|
|
|
|
|
|
for &target in targets {
|
2016-05-26 05:11:16 -05:00
|
|
|
node_states[query.indices[target].0] = State::Included;
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
|
2016-05-26 05:11:16 -05:00
|
|
|
for source in sources.iter().map(|&n| query.indices[n]) {
|
2015-12-22 16:17:27 -06:00
|
|
|
recurse(query, &mut node_states, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
return query
|
|
|
|
.nodes()
|
|
|
|
.into_iter()
|
2016-05-26 05:11:16 -05:00
|
|
|
.filter(|&n| {
|
2015-12-22 16:17:27 -06:00
|
|
|
let index = query.indices[n];
|
|
|
|
node_states[index.0] == State::Included
|
|
|
|
})
|
2022-03-28 08:59:37 -05:00
|
|
|
.map(|n| n.kind)
|
2015-12-22 16:17:27 -06:00
|
|
|
.collect();
|
2019-12-22 16:42:04 -06:00
|
|
|
|
2017-06-02 10:36:30 -05:00
|
|
|
fn recurse(query: &DepGraphQuery, node_states: &mut [State], node: NodeIndex) -> bool {
|
2015-12-22 16:17:27 -06:00
|
|
|
match node_states[node.0] {
|
|
|
|
// known to reach a target
|
|
|
|
State::Included => return true,
|
|
|
|
|
|
|
|
// known not to reach a target
|
|
|
|
State::Excluded => return false,
|
|
|
|
|
|
|
|
// backedge, not yet known, say false
|
|
|
|
State::Deciding => return false,
|
|
|
|
|
|
|
|
State::Undecided => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
node_states[node.0] = State::Deciding;
|
|
|
|
|
|
|
|
for neighbor_index in query.graph.successor_nodes(node) {
|
|
|
|
if recurse(query, node_states, neighbor_index) {
|
|
|
|
node_states[node.0] = State::Included;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we didn't find a path to target, then set to excluded
|
|
|
|
if node_states[node.0] == State::Deciding {
|
|
|
|
node_states[node.0] = State::Excluded;
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
assert!(node_states[node.0] == State::Included);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:36:30 -05:00
|
|
|
fn filter_edges<'q>(
|
|
|
|
query: &'q DepGraphQuery,
|
2022-03-28 08:59:37 -05:00
|
|
|
nodes: &FxHashSet<DepKind>,
|
|
|
|
) -> Vec<(DepKind, DepKind)> {
|
|
|
|
let uniq: FxHashSet<_> = query
|
2015-12-22 16:17:27 -06:00
|
|
|
.edges()
|
|
|
|
.into_iter()
|
2022-03-28 08:59:37 -05:00
|
|
|
.map(|(s, t)| (s.kind, t.kind))
|
|
|
|
.filter(|(source, target)| nodes.contains(source) && nodes.contains(target))
|
|
|
|
.collect();
|
|
|
|
uniq.into_iter().collect()
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|