Use config::pub_only rather than a spearate api mode

This commit is contained in:
Nick Cameron 2017-07-19 11:52:09 +12:00
parent 4d81e8bb22
commit 52e9f2035a
6 changed files with 51 additions and 161 deletions

View File

@ -960,9 +960,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
save_analysis: bool = (false, parse_bool, [UNTRACKED],
"write syntax and type analysis (in JSON format) information, in \
addition to normal output"),
save_analysis_api: bool = (false, parse_bool, [UNTRACKED],
"write syntax and type analysis information for opaque libraries (in JSON format), \
in addition to normal output"),
print_move_fragments: bool = (false, parse_bool, [UNTRACKED],
"print out move-fragment data for every fn"),
flowgraph_print_loans: bool = (false, parse_bool, [UNTRACKED],
@ -2527,8 +2524,6 @@ mod tests {
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.debugging_opts.save_analysis = true;
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.debugging_opts.save_analysis_api = true;
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.debugging_opts.print_move_fragments = true;
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.debugging_opts.flowgraph_print_loans = true;

View File

@ -579,8 +579,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
state.analysis.unwrap(),
state.crate_name.unwrap(),
None,
DumpHandler::new(save_analysis_format(state.session),
state.out_dir,
DumpHandler::new(state.out_dir,
state.crate_name.unwrap()))
});
};
@ -603,18 +602,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
}
fn save_analysis(sess: &Session) -> bool {
sess.opts.debugging_opts.save_analysis ||
sess.opts.debugging_opts.save_analysis_api
}
fn save_analysis_format(sess: &Session) -> save::Format {
if sess.opts.debugging_opts.save_analysis {
save::Format::Json
} else if sess.opts.debugging_opts.save_analysis_api {
save::Format::JsonApi
} else {
unreachable!();
}
sess.opts.debugging_opts.save_analysis
}
impl RustcDefaultCalls {

View File

@ -41,7 +41,8 @@ use syntax::ptr::P;
use syntax::codemap::Spanned;
use syntax_pos::*;
use {escape, generated_code, SaveContext, PathCollector, lower_attributes, Dump};
use {escape, generated_code, SaveContext, PathCollector, lower_attributes};
use json_dumper::{JsonDumper, DumpOutput};
use span_utils::SpanUtils;
use sig;
@ -58,11 +59,11 @@ macro_rules! down_cast_data {
};
}
pub struct DumpVisitor<'l, 'tcx: 'l, 'll, D: 'll> {
pub struct DumpVisitor<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> {
save_ctxt: SaveContext<'l, 'tcx>,
sess: &'l Session,
tcx: TyCtxt<'l, 'tcx, 'tcx>,
dumper: &'ll mut D,
dumper: &'ll mut JsonDumper<O>,
span: SpanUtils<'l>,
@ -75,10 +76,10 @@ pub struct DumpVisitor<'l, 'tcx: 'l, 'll, D: 'll> {
// mac_defs: HashSet<Span>,
}
impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
pub fn new(save_ctxt: SaveContext<'l, 'tcx>,
dumper: &'ll mut D)
-> DumpVisitor<'l, 'tcx, 'll, D> {
dumper: &'ll mut JsonDumper<O>)
-> DumpVisitor<'l, 'tcx, 'll, O> {
let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
DumpVisitor {
sess: &save_ctxt.tcx.sess,
@ -92,7 +93,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
}
fn nest_scope<F>(&mut self, scope_id: NodeId, f: F)
where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, D>)
where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>)
{
let parent_scope = self.cur_scope;
self.cur_scope = scope_id;
@ -101,7 +102,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
}
fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, D>)
where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>)
{
let item_def_id = self.tcx.hir.local_def_id(item_id);
if self.tcx.has_typeck_tables(item_def_id) {
@ -1089,7 +1090,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
}
}
impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, D> {
impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, O> {
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.

View File

@ -1,66 +0,0 @@
// Copyright 2016 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.
use std::io::Write;
use rustc_serialize::json::as_json;
use Dump;
use rls_data::{Analysis, Import, Def, CratePreludeData, Format, Relation};
// A dumper to dump a restricted set of JSON information, designed for use with
// libraries distributed without their source. Clients are likely to use type
// information here, and (for example) generate Rustdoc URLs, but don't need
// information for navigating the source of the crate.
// Relative to the regular JSON save-analysis info, this form is filtered to
// remove non-visible items.
pub struct JsonApiDumper<'b, W: Write + 'b> {
output: &'b mut W,
result: Analysis,
}
impl<'b, W: Write> JsonApiDumper<'b, W> {
pub fn new(writer: &'b mut W) -> JsonApiDumper<'b, W> {
let mut result = Analysis::new();
result.kind = Format::JsonApi;
JsonApiDumper { output: writer, result }
}
}
impl<'b, W: Write> Drop for JsonApiDumper<'b, W> {
fn drop(&mut self) {
if let Err(_) = write!(self.output, "{}", as_json(&self.result)) {
error!("Error writing output");
}
}
}
impl<'b, W: Write + 'b> Dump for JsonApiDumper<'b, W> {
fn crate_prelude(&mut self, data: CratePreludeData) {
self.result.prelude = Some(data)
}
fn dump_relation(&mut self, data: Relation) {
self.result.relations.push(data);
}
fn import(&mut self, public: bool, import: Import) {
if public {
self.result.imports.push(import);
}
}
fn dump_def(&mut self, public: bool, mut data: Def) {
if public {
data.attributes = vec![];
self.result.defs.push(data);
}
}
}

View File

@ -14,12 +14,12 @@ use rustc_serialize::json::as_json;
use rls_data::{self, Analysis, Import, Def, DefKind, Ref, RefKind, MacroRef,
Relation, CratePreludeData};
use rls_data::config::Config;
use rls_span::{Column, Row};
use Dump;
pub struct JsonDumper<O: DumpOutput> {
result: Analysis,
config: Config,
output: O,
}
@ -50,14 +50,14 @@ impl<'b> DumpOutput for CallbackOutput<'b> {
}
impl<'b, W: Write> JsonDumper<WriteOutput<'b, W>> {
pub fn new(writer: &'b mut W) -> JsonDumper<WriteOutput<'b, W>> {
JsonDumper { output: WriteOutput { output: writer }, result: Analysis::new() }
pub fn new(writer: &'b mut W, config: Config) -> JsonDumper<WriteOutput<'b, W>> {
JsonDumper { output: WriteOutput { output: writer }, config, result: Analysis::new() }
}
}
impl<'b> JsonDumper<CallbackOutput<'b>> {
pub fn with_callback(callback: &'b mut FnMut(&Analysis)) -> JsonDumper<CallbackOutput<'b>> {
JsonDumper { output: CallbackOutput { callback: callback }, result: Analysis::new() }
pub fn with_callback(callback: &'b mut FnMut(&Analysis), config: Config) -> JsonDumper<CallbackOutput<'b>> {
JsonDumper { output: CallbackOutput { callback: callback }, config, result: Analysis::new() }
}
}
@ -67,23 +67,36 @@ impl<O: DumpOutput> Drop for JsonDumper<O> {
}
}
impl<'b, O: DumpOutput + 'b> Dump for JsonDumper<O> {
fn crate_prelude(&mut self, data: CratePreludeData) {
impl<'b, O: DumpOutput + 'b> JsonDumper<O> {
pub fn crate_prelude(&mut self, data: CratePreludeData) {
self.result.prelude = Some(data)
}
fn macro_use(&mut self, data: MacroRef) {
pub fn macro_use(&mut self, data: MacroRef) {
if self.config.pub_only {
return;
}
self.result.macro_refs.push(data);
}
fn import(&mut self, _: bool, import: Import) {
pub fn import(&mut self, public: bool, import: Import) {
if !public && self.config.pub_only {
return;
}
self.result.imports.push(import);
}
fn dump_ref(&mut self, data: Ref) {
pub fn dump_ref(&mut self, data: Ref) {
if self.config.pub_only {
return;
}
self.result.refs.push(data);
}
fn dump_def(&mut self, _: bool, mut data: Def) {
pub fn dump_def(&mut self, public: bool, mut data: Def) {
if !public && self.config.pub_only {
return;
}
if data.kind == DefKind::Mod && data.span.file_name.to_str().unwrap() != data.value {
// If the module is an out-of-line defintion, then we'll make the
// defintion the first character in the module's file and turn the
@ -107,7 +120,7 @@ impl<'b, O: DumpOutput + 'b> Dump for JsonDumper<O> {
self.result.defs.push(data);
}
fn dump_relation(&mut self, data: Relation) {
pub fn dump_relation(&mut self, data: Relation) {
self.result.relations.push(data);
}
}

View File

@ -31,7 +31,6 @@ extern crate rls_data;
extern crate rls_span;
mod json_api_dumper;
mod json_dumper;
mod dump_visitor;
#[macro_use]
@ -61,13 +60,12 @@ use syntax::print::pprust::{ty_to_string, arg_to_string};
use syntax::codemap::MacroAttribute;
use syntax_pos::*;
pub use json_api_dumper::JsonApiDumper;
pub use json_dumper::JsonDumper;
use json_dumper::JsonDumper;
use dump_visitor::DumpVisitor;
use span_utils::SpanUtils;
use rls_data::{Ref, RefKind, SpanData, MacroRef, Def, DefKind, Relation, RelationKind,
ExternalCrateData, Import, CratePreludeData};
ExternalCrateData};
use rls_data::config::Config;
@ -88,15 +86,6 @@ pub enum Data {
RelationData(Relation),
}
pub trait Dump {
fn crate_prelude(&mut self, _: CratePreludeData);
fn macro_use(&mut self, _: MacroRef) {}
fn import(&mut self, _: bool, _: Import);
fn dump_ref(&mut self, _: Ref) {}
fn dump_def(&mut self, _: bool, _: Def);
fn dump_relation(&mut self, data: Relation);
}
macro_rules! option_try(
($e:expr) => (match $e { Some(e) => e, None => return None })
);
@ -872,18 +861,6 @@ impl<'a> Visitor<'a> for PathCollector {
}
}
#[derive(Clone, Copy, Debug, RustcEncodable)]
pub enum Format {
Json,
JsonApi,
}
impl Format {
fn extension(&self) -> &'static str {
".json"
}
}
/// Defines what to do with the results of saving the analysis.
pub trait SaveHandler {
fn save<'l, 'tcx>(&mut self,
@ -894,15 +871,13 @@ pub trait SaveHandler {
/// Dump the save-analysis results to a file.
pub struct DumpHandler<'a> {
format: Format,
odir: Option<&'a Path>,
cratename: String
}
impl<'a> DumpHandler<'a> {
pub fn new(format: Format, odir: Option<&'a Path>, cratename: &str) -> DumpHandler<'a> {
pub fn new(odir: Option<&'a Path>, cratename: &str) -> DumpHandler<'a> {
DumpHandler {
format: format,
odir: odir,
cratename: cratename.to_owned()
}
@ -930,7 +905,7 @@ impl<'a> DumpHandler<'a> {
};
out_name.push_str(&self.cratename);
out_name.push_str(&sess.opts.cg.extra_filename);
out_name.push_str(self.format.extension());
out_name.push_str(".json");
root_path.push(&out_name);
root_path
@ -952,22 +927,12 @@ impl<'a> SaveHandler for DumpHandler<'a> {
save_ctxt: SaveContext<'l, 'tcx>,
krate: &ast::Crate,
cratename: &str) {
macro_rules! dump {
($new_dumper: expr) => {{
let mut dumper = $new_dumper;
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
visitor.dump_crate_info(cratename, krate);
visit::walk_crate(&mut visitor, krate);
}}
}
let output = &mut self.output_file(&save_ctxt);
let mut dumper = JsonDumper::new(output, save_ctxt.config.clone());
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
match self.format {
Format::Json => dump!(JsonDumper::new(output)),
Format::JsonApi => dump!(JsonApiDumper::new(output)),
}
visitor.dump_crate_info(cratename, krate);
visit::walk_crate(&mut visitor, krate);
}
}
@ -981,22 +946,16 @@ impl<'b> SaveHandler for CallbackHandler<'b> {
save_ctxt: SaveContext<'l, 'tcx>,
krate: &ast::Crate,
cratename: &str) {
macro_rules! dump {
($new_dumper: expr) => {{
let mut dumper = $new_dumper;
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
visitor.dump_crate_info(cratename, krate);
visit::walk_crate(&mut visitor, krate);
}}
}
// We're using the JsonDumper 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
// 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).
dump!(JsonDumper::with_callback(self.callback))
let mut dumper = JsonDumper::with_callback(self.callback, save_ctxt.config.clone());
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
visitor.dump_crate_info(cratename, krate);
visit::walk_crate(&mut visitor, krate);
}
}