2015-12-22 16:17:27 -06:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! 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
|
2016-03-28 16:36:56 -05:00
|
|
|
//! exist in the graph. These checks run after trans, so they view the
|
|
|
|
//! 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
|
|
|
|
//! each case where no path exists. `compile-fail` tests can then be
|
|
|
|
//! 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:
|
|
|
|
//!
|
|
|
|
//! ```
|
2016-08-24 10:00:55 -05:00
|
|
|
//! #[rustc_if_this_changed(Hir)]
|
2015-12-22 16:17:27 -06:00
|
|
|
//! fn foo() { }
|
|
|
|
//!
|
2016-08-24 10:00:55 -05:00
|
|
|
//! #[rustc_then_this_would_need(trans)] //~ ERROR no path from `foo`
|
2015-12-22 16:17:27 -06:00
|
|
|
//! fn bar() { }
|
|
|
|
//!
|
2016-08-24 10:00:55 -05:00
|
|
|
//! #[rustc_then_this_would_need(trans)] //~ ERROR OK
|
2015-12-22 16:17:27 -06:00
|
|
|
//! fn baz() { foo(); }
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
use graphviz as dot;
|
|
|
|
use rustc::dep_graph::{DepGraphQuery, DepNode};
|
2016-05-06 04:02:05 -05:00
|
|
|
use rustc::dep_graph::debug::{DepNodeFilter, EdgeFilter};
|
2016-03-29 04:54:26 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::TyCtxt;
|
2016-08-24 10:00:55 -05:00
|
|
|
use rustc_data_structures::fnv::FnvHashSet;
|
2015-12-22 16:17:27 -06:00
|
|
|
use rustc_data_structures::graph::{Direction, INCOMING, OUTGOING, NodeIndex};
|
2016-03-29 00:50:44 -05:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::intravisit::Visitor;
|
2016-01-15 12:07:52 -06:00
|
|
|
use graphviz::IntoCow;
|
2015-12-22 16:17:27 -06:00
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::parse::token::InternedString;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::Span;
|
2016-08-30 15:49:54 -05:00
|
|
|
use {ATTR_IF_THIS_CHANGED, ATTR_THEN_THIS_WOULD_NEED};
|
2015-12-22 16:17:27 -06:00
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn assert_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
2015-12-22 16:17:27 -06:00
|
|
|
let _ignore = tcx.dep_graph.in_ignore();
|
|
|
|
|
2016-03-28 16:36:56 -05:00
|
|
|
if tcx.sess.opts.debugging_opts.dump_dep_graph {
|
2015-12-22 16:17:27 -06:00
|
|
|
dump_graph(tcx);
|
|
|
|
}
|
|
|
|
|
2016-08-03 18:33:51 -05:00
|
|
|
// if the `rustc_attrs` feature is not enabled, then the
|
|
|
|
// attributes we are interested in cannot be present anyway, so
|
|
|
|
// skip the walk.
|
|
|
|
if !tcx.sess.features.borrow().rustc_attrs {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-22 16:17:27 -06:00
|
|
|
// Find annotations supplied by user (if any).
|
|
|
|
let (if_this_changed, then_this_would_need) = {
|
|
|
|
let mut visitor = IfThisChanged { tcx: tcx,
|
2016-08-24 10:00:55 -05:00
|
|
|
if_this_changed: vec![],
|
|
|
|
then_this_would_need: vec![] };
|
|
|
|
visitor.process_attrs(ast::CRATE_NODE_ID, &tcx.map.krate().attrs);
|
2015-12-22 16:17:27 -06:00
|
|
|
tcx.map.krate().visit_all_items(&mut visitor);
|
|
|
|
(visitor.if_this_changed, visitor.then_this_would_need)
|
|
|
|
};
|
|
|
|
|
2016-03-28 16:36:56 -05:00
|
|
|
if !if_this_changed.is_empty() || !then_this_would_need.is_empty() {
|
|
|
|
assert!(tcx.sess.opts.debugging_opts.query_dep_graph,
|
|
|
|
"cannot use the `#[{}]` or `#[{}]` annotations \
|
|
|
|
without supplying `-Z query-dep-graph`",
|
2016-08-30 15:49:54 -05:00
|
|
|
ATTR_IF_THIS_CHANGED, ATTR_THEN_THIS_WOULD_NEED);
|
2016-03-28 16:36:56 -05:00
|
|
|
}
|
|
|
|
|
2015-12-22 16:17:27 -06:00
|
|
|
// Check paths.
|
|
|
|
check_paths(tcx, &if_this_changed, &then_this_would_need);
|
|
|
|
}
|
|
|
|
|
2016-08-24 10:00:55 -05:00
|
|
|
type Sources = Vec<(Span, DefId, DepNode<DefId>)>;
|
|
|
|
type Targets = Vec<(Span, InternedString, ast::NodeId, DepNode<DefId>)>;
|
2015-12-22 16:17:27 -06:00
|
|
|
|
|
|
|
struct IfThisChanged<'a, 'tcx:'a> {
|
2016-05-02 21:23:22 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, '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
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> IfThisChanged<'a, 'tcx> {
|
2016-08-24 10:00:55 -05:00
|
|
|
fn argument(&self, attr: &ast::Attribute) -> Option<InternedString> {
|
|
|
|
let mut value = None;
|
|
|
|
for list_item in attr.meta_item_list().unwrap_or_default() {
|
|
|
|
match list_item.word() {
|
|
|
|
Some(word) if value.is_none() =>
|
|
|
|
value = Some(word.name().clone()),
|
|
|
|
_ =>
|
|
|
|
// FIXME better-encapsulate meta_item (don't directly access `node`)
|
|
|
|
span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item.node),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_attrs(&mut self, node_id: ast::NodeId, attrs: &[ast::Attribute]) {
|
|
|
|
let def_id = self.tcx.map.local_def_id(node_id);
|
|
|
|
for attr in attrs {
|
2016-08-30 15:49:54 -05:00
|
|
|
if attr.check_name(ATTR_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 {
|
|
|
|
None => DepNode::Hir(def_id),
|
|
|
|
Some(ref n) => {
|
|
|
|
match DepNode::from_label_string(&n[..], def_id) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(()) => {
|
|
|
|
self.tcx.sess.span_fatal(
|
|
|
|
attr.span,
|
|
|
|
&format!("unrecognized DepNode variant {:?}", n));
|
|
|
|
}
|
2016-08-19 20:58:14 -05:00
|
|
|
}
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
2016-08-24 10:00:55 -05:00
|
|
|
};
|
|
|
|
self.if_this_changed.push((attr.span, def_id, dep_node));
|
2016-08-30 15:49:54 -05:00
|
|
|
} else if attr.check_name(ATTR_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 {
|
|
|
|
Some(ref n) => {
|
|
|
|
match DepNode::from_label_string(&n[..], def_id) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(()) => {
|
|
|
|
self.tcx.sess.span_fatal(
|
|
|
|
attr.span,
|
|
|
|
&format!("unrecognized DepNode variant {:?}", n));
|
|
|
|
}
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 16:36:56 -05:00
|
|
|
None => {
|
2015-12-22 16:17:27 -06:00
|
|
|
self.tcx.sess.span_fatal(
|
|
|
|
attr.span,
|
2016-03-28 16:36:56 -05:00
|
|
|
&format!("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,
|
|
|
|
dep_node_interned.clone().unwrap(),
|
|
|
|
node_id,
|
|
|
|
dep_node));
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for IfThisChanged<'a, 'tcx> {
|
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
2016-08-24 10:00:55 -05:00
|
|
|
self.process_attrs(item.id, &item.attrs);
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
fn check_paths<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, '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
|
|
|
{
|
|
|
|
// 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 {
|
|
|
|
tcx.sess.span_err(
|
|
|
|
target_span,
|
|
|
|
&format!("no #[rustc_if_this_changed] annotation detected"));
|
|
|
|
|
|
|
|
}
|
2015-12-22 16:17:27 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let query = tcx.dep_graph.query();
|
2016-08-24 10:00:55 -05:00
|
|
|
for &(_, source_def_id, ref source_dep_node) in if_this_changed {
|
|
|
|
let dependents = query.transitive_successors(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.item_path_str(source_def_id),
|
|
|
|
target_pass));
|
|
|
|
} else {
|
|
|
|
tcx.sess.span_err(
|
|
|
|
target_span,
|
|
|
|
&format!("OK"));
|
2015-12-22 16:17:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 20:56:42 -05:00
|
|
|
fn dump_graph(tcx: TyCtxt) {
|
2015-12-22 16:17:27 -06:00
|
|
|
let path: String = env::var("RUST_DEP_GRAPH").unwrap_or_else(|_| format!("dep_graph"));
|
|
|
|
let query = tcx.dep_graph.query();
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
query.nodes()
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let edges = filter_edges(&query, &nodes);
|
|
|
|
|
|
|
|
{ // dump a .txt file with just the edges:
|
|
|
|
let txt_path = format!("{}.txt", path);
|
|
|
|
let mut file = 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();
|
|
|
|
File::create(&dot_path).and_then(|mut f| f.write_all(&v)).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 05:11:16 -05:00
|
|
|
pub struct GraphvizDepGraph<'q>(FnvHashSet<&'q DepNode<DefId>>,
|
|
|
|
Vec<(&'q DepNode<DefId>, &'q DepNode<DefId>)>);
|
2015-12-22 16:17:27 -06:00
|
|
|
|
2016-05-26 05:11:16 -05:00
|
|
|
impl<'a, 'tcx, 'q> dot::GraphWalk<'a> for GraphvizDepGraph<'q> {
|
|
|
|
type Node = &'q DepNode<DefId>;
|
|
|
|
type Edge = (&'q DepNode<DefId>, &'q DepNode<DefId>);
|
|
|
|
fn nodes(&self) -> dot::Nodes<&'q DepNode<DefId>> {
|
2015-12-22 16:17:27 -06:00
|
|
|
let nodes: Vec<_> = self.0.iter().cloned().collect();
|
|
|
|
nodes.into_cow()
|
|
|
|
}
|
2016-05-26 05:11:16 -05:00
|
|
|
fn edges(&self) -> dot::Edges<(&'q DepNode<DefId>, &'q DepNode<DefId>)> {
|
2015-12-22 16:17:27 -06:00
|
|
|
self.1[..].into_cow()
|
|
|
|
}
|
2016-05-26 05:11:16 -05:00
|
|
|
fn source(&self, edge: &(&'q DepNode<DefId>, &'q DepNode<DefId>)) -> &'q DepNode<DefId> {
|
2015-12-22 16:17:27 -06:00
|
|
|
edge.0
|
|
|
|
}
|
2016-05-26 05:11:16 -05:00
|
|
|
fn target(&self, edge: &(&'q DepNode<DefId>, &'q DepNode<DefId>)) -> &'q DepNode<DefId> {
|
2015-12-22 16:17:27 -06:00
|
|
|
edge.1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 05:11:16 -05:00
|
|
|
impl<'a, 'tcx, 'q> dot::Labeller<'a> for GraphvizDepGraph<'q> {
|
|
|
|
type Node = &'q DepNode<DefId>;
|
|
|
|
type Edge = (&'q DepNode<DefId>, &'q DepNode<DefId>);
|
2015-12-22 16:17:27 -06:00
|
|
|
fn graph_id(&self) -> dot::Id {
|
|
|
|
dot::Id::new("DependencyGraph").unwrap()
|
|
|
|
}
|
2016-05-26 05:11:16 -05:00
|
|
|
fn node_id(&self, n: &&'q DepNode<DefId>) -> 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()
|
|
|
|
}
|
2016-05-26 05:11:16 -05:00
|
|
|
fn node_label(&self, n: &&'q DepNode<DefId>) -> 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.
|
2016-05-26 05:11:16 -05:00
|
|
|
fn node_set<'q>(query: &'q DepGraphQuery<DefId>, filter: &DepNodeFilter)
|
|
|
|
-> Option<FnvHashSet<&'q DepNode<DefId>>>
|
2016-03-28 16:36:56 -05:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2016-05-26 05:11:16 -05:00
|
|
|
fn filter_nodes<'q>(query: &'q DepGraphQuery<DefId>,
|
|
|
|
sources: &Option<FnvHashSet<&'q DepNode<DefId>>>,
|
|
|
|
targets: &Option<FnvHashSet<&'q DepNode<DefId>>>)
|
|
|
|
-> FnvHashSet<&'q DepNode<DefId>>
|
2015-12-22 16:17:27 -06:00
|
|
|
{
|
|
|
|
if let &Some(ref sources) = sources {
|
|
|
|
if let &Some(ref targets) = targets {
|
|
|
|
walk_between(query, sources, targets)
|
|
|
|
} else {
|
|
|
|
walk_nodes(query, sources, OUTGOING)
|
|
|
|
}
|
|
|
|
} else if let &Some(ref targets) = targets {
|
|
|
|
walk_nodes(query, targets, INCOMING)
|
|
|
|
} else {
|
|
|
|
query.nodes().into_iter().collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 05:11:16 -05:00
|
|
|
fn walk_nodes<'q>(query: &'q DepGraphQuery<DefId>,
|
|
|
|
starts: &FnvHashSet<&'q DepNode<DefId>>,
|
|
|
|
direction: Direction)
|
|
|
|
-> FnvHashSet<&'q DepNode<DefId>>
|
2015-12-22 16:17:27 -06:00
|
|
|
{
|
|
|
|
let mut set = FnvHashSet();
|
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);
|
2016-05-26 05:11:16 -05:00
|
|
|
if set.insert(start) {
|
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);
|
2016-05-26 05:11:16 -05:00
|
|
|
if set.insert(neighbor) {
|
2015-12-22 16:17:27 -06:00
|
|
|
stack.push(neighbor_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set
|
|
|
|
}
|
|
|
|
|
2016-05-26 05:11:16 -05:00
|
|
|
fn walk_between<'q>(query: &'q DepGraphQuery<DefId>,
|
|
|
|
sources: &FnvHashSet<&'q DepNode<DefId>>,
|
|
|
|
targets: &FnvHashSet<&'q DepNode<DefId>>)
|
|
|
|
-> FnvHashSet<&'q DepNode<DefId>>
|
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
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-03-28 16:36:56 -05:00
|
|
|
fn recurse(query: &DepGraphQuery<DefId>,
|
2015-12-22 16:17:27 -06:00
|
|
|
node_states: &mut [State],
|
|
|
|
node: NodeIndex)
|
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 05:11:16 -05:00
|
|
|
fn filter_edges<'q>(query: &'q DepGraphQuery<DefId>,
|
|
|
|
nodes: &FnvHashSet<&'q DepNode<DefId>>)
|
|
|
|
-> Vec<(&'q DepNode<DefId>, &'q DepNode<DefId>)>
|
2015-12-22 16:17:27 -06:00
|
|
|
{
|
|
|
|
query.edges()
|
|
|
|
.into_iter()
|
2016-05-26 05:11:16 -05:00
|
|
|
.filter(|&(source, target)| nodes.contains(source) && nodes.contains(target))
|
2015-12-22 16:17:27 -06:00
|
|
|
.collect()
|
|
|
|
}
|