Skip to content

Commit

Permalink
Rollup merge of rust-lang#62979 - Mark-Simulacrum:json-dumper-pretty,…
Browse files Browse the repository at this point in the history
… r=Xanewok

Cleanup save-analysis JsonDumper
  • Loading branch information
Centril authored Jul 26, 2019
2 parents 75d3a29 + 68c0ba2 commit b3e2164
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 78 deletions.
20 changes: 10 additions & 10 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//!
//! SpanUtils is used to manipulate spans. In particular, to extract sub-spans
//! from spans (e.g., the span for `bar` from the above example path).
//! DumpVisitor walks the AST and processes it, and JsonDumper is used for
//! DumpVisitor walks the AST and processes it, and Dumper is used for
//! recording the output.

use rustc::hir::def::{Res, DefKind as HirDefKind};
Expand Down Expand Up @@ -38,7 +38,7 @@ use syntax_pos::*;

use crate::{escape, generated_code, id_from_def_id, id_from_node_id, lower_attributes,
PathCollector, SaveContext};
use crate::json_dumper::{Access, DumpOutput, JsonDumper};
use crate::dumper::{Access, Dumper};
use crate::span_utils::SpanUtils;
use crate::sig;

Expand Down Expand Up @@ -75,10 +75,10 @@ macro_rules! access_from_vis {
};
}

pub struct DumpVisitor<'l, 'tcx, 'll, O: DumpOutput> {
pub struct DumpVisitor<'l, 'tcx, 'll> {
save_ctxt: SaveContext<'l, 'tcx>,
tcx: TyCtxt<'tcx>,
dumper: &'ll mut JsonDumper<O>,
dumper: &'ll mut Dumper,

span: SpanUtils<'l>,

Expand All @@ -92,11 +92,11 @@ pub struct DumpVisitor<'l, 'tcx, 'll, O: DumpOutput> {
// macro_calls: FxHashSet<Span>,
}

impl<'l, 'tcx, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
impl<'l, 'tcx, 'll> DumpVisitor<'l, 'tcx, 'll> {
pub fn new(
save_ctxt: SaveContext<'l, 'tcx>,
dumper: &'ll mut JsonDumper<O>,
) -> DumpVisitor<'l, 'tcx, 'll, O> {
dumper: &'ll mut Dumper,
) -> DumpVisitor<'l, 'tcx, 'll> {
let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
DumpVisitor {
tcx: save_ctxt.tcx,
Expand All @@ -111,7 +111,7 @@ impl<'l, 'tcx, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {

fn nest_scope<F>(&mut self, scope_id: NodeId, f: F)
where
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>),
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll>),
{
let parent_scope = self.cur_scope;
self.cur_scope = scope_id;
Expand All @@ -121,7 +121,7 @@ impl<'l, 'tcx, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {

fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
where
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>),
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll>),
{
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id);
if self.tcx.has_typeck_tables(item_def_id) {
Expand Down Expand Up @@ -1311,7 +1311,7 @@ impl<'l, 'tcx, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
}
}

impl<'l, 'tcx, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, O> {
impl<'l, 'tcx, 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll> {
fn visit_mod(&mut self, m: &'l ast::Mod, span: Span, attrs: &[ast::Attribute], id: NodeId) {
// Since we handle explicit modules ourselves in visit_item, this should
// only get called for the root module of a crate.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,80 +1,33 @@
use std::io::Write;

use rls_data::config::Config;
use rls_data::{self, Analysis, CompilationOptions, CratePreludeData, Def, DefKind, Impl, Import,
MacroRef, Ref, RefKind, Relation};
use rls_span::{Column, Row};

use log::error;

#[derive(Debug)]
pub struct Access {
pub reachable: bool,
pub public: bool,
}

pub struct JsonDumper<O: DumpOutput> {
pub struct Dumper {
result: Analysis,
config: Config,
output: O,
}

pub trait DumpOutput {
fn dump(&mut self, result: &Analysis);
}

pub struct WriteOutput<'b, W: Write> {
output: &'b mut W,
}

impl<'b, W: Write> DumpOutput for WriteOutput<'b, W> {
fn dump(&mut self, result: &Analysis) {
if let Err(e) = serde_json::to_writer(self.output.by_ref(), result) {
error!("Can't serialize save-analysis: {:?}", e);
}
}
}

pub struct CallbackOutput<'b> {
callback: &'b mut dyn FnMut(&Analysis),
}

impl<'b> DumpOutput for CallbackOutput<'b> {
fn dump(&mut self, result: &Analysis) {
(self.callback)(result)
}
}

impl<'b, W: Write> JsonDumper<WriteOutput<'b, W>> {
pub fn new(writer: &'b mut W, config: Config) -> JsonDumper<WriteOutput<'b, W>> {
JsonDumper {
output: WriteOutput { output: writer },
impl Dumper {
pub fn new(config: Config) -> Dumper {
Dumper {
config: config.clone(),
result: Analysis::new(config),
}
}
}

impl<'b> JsonDumper<CallbackOutput<'b>> {
pub fn with_callback(
callback: &'b mut dyn FnMut(&Analysis),
config: Config,
) -> JsonDumper<CallbackOutput<'b>> {
JsonDumper {
output: CallbackOutput { callback },
config: config.clone(),
result: Analysis::new(config),
}
}
}

impl<O: DumpOutput> Drop for JsonDumper<O> {
fn drop(&mut self) {
self.output.dump(&self.result);
pub fn to_output(self, f: impl FnOnce(&Analysis)) {
f(&self.result)
}
}

impl<'b, O: DumpOutput + 'b> JsonDumper<O> {
impl Dumper {
pub fn crate_prelude(&mut self, data: CratePreludeData) {
self.result.prelude = Some(data)
}
Expand Down
32 changes: 18 additions & 14 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#![recursion_limit="256"]


mod json_dumper;
mod dumper;
mod dump_visitor;
#[macro_use]
mod span_utils;
Expand Down Expand Up @@ -39,7 +39,7 @@ use syntax::visit::{self, Visitor};
use syntax::print::pprust::{arg_to_string, ty_to_string};
use syntax_pos::*;

use json_dumper::JsonDumper;
use dumper::Dumper;
use dump_visitor::DumpVisitor;
use span_utils::SpanUtils;

Expand Down Expand Up @@ -1075,17 +1075,19 @@ impl<'a> SaveHandler for DumpHandler<'a> {
input: &'l Input,
) {
let sess = &save_ctxt.tcx.sess;
let file_name = {
let (mut output, file_name) = self.output_file(&save_ctxt);
let mut dumper = JsonDumper::new(&mut output, save_ctxt.config.clone());
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
let (output, file_name) = self.output_file(&save_ctxt);
let mut dumper = Dumper::new(save_ctxt.config.clone());
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);

visitor.dump_crate_info(cratename, krate);
visitor.dump_compilation_options(input, cratename);
visit::walk_crate(&mut visitor, krate);
visitor.dump_crate_info(cratename, krate);
visitor.dump_compilation_options(input, cratename);
visit::walk_crate(&mut visitor, krate);

file_name
};
dumper.to_output(|analysis| {
if let Err(e) = serde_json::to_writer(output, analysis) {
error!("Can't serialize save-analysis: {:?}", e);
}
});

if sess.opts.debugging_opts.emit_artifact_notifications {
sess.parse_sess.span_diagnostic
Expand All @@ -1107,17 +1109,19 @@ impl<'b> SaveHandler for CallbackHandler<'b> {
cratename: &str,
input: &'l Input,
) {
// We're using the JsonDumper here because it has the format of the
// We're using the Dumper here because it has the format of the
// save-analysis results that we will pass to the callback. IOW, we are
// using the JsonDumper to collect the save-analysis results, but not
// using the Dumper to collect the save-analysis results, but not
// actually to dump them to a file. This is all a bit convoluted and
// there is certainly a simpler design here trying to get out (FIXME).
let mut dumper = JsonDumper::with_callback(self.callback, save_ctxt.config.clone());
let mut dumper = Dumper::new(save_ctxt.config.clone());
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);

visitor.dump_crate_info(cratename, krate);
visitor.dump_compilation_options(input, cratename);
visit::walk_crate(&mut visitor, krate);

dumper.to_output(|a| (self.callback)(a))
}
}

Expand Down

0 comments on commit b3e2164

Please sign in to comment.