From 66702b456cc4997f8118a8085c62481d94019bb7 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 25 Jul 2017 18:46:14 +1200 Subject: [PATCH 1/4] save-analysis: dedup macro references --- src/librustc_save_analysis/dump_visitor.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index ebdd99dc802..ca27bd76fff 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -30,6 +30,7 @@ use rustc::hir::map::Node; use rustc::session::Session; use rustc::ty::{self, TyCtxt}; +use std::collections::HashSet; use std::path::Path; use syntax::ast::{self, NodeId, PatKind, Attribute, CRATE_NODE_ID}; @@ -74,6 +75,7 @@ pub struct DumpVisitor<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> { // we only write one macro def per unique macro definition, and // one macro use per unique callsite span. // mac_defs: HashSet, + macro_calls: HashSet, } impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { @@ -89,6 +91,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { span: span_utils.clone(), cur_scope: CRATE_NODE_ID, // mac_defs: HashSet::new(), + macro_calls: HashSet::new(), } } @@ -972,11 +975,19 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { /// callsite spans to record macro definition and use data, using the /// mac_uses and mac_defs sets to prevent multiples. fn process_macro_use(&mut self, span: Span) { + let source_span = span.source_callsite(); + if self.macro_calls.contains(&source_span) { + return; + } + self.macro_calls.insert(source_span); + let data = match self.save_ctxt.get_macro_use_data(span) { None => return, Some(data) => data, }; + self.dumper.macro_use(data); + // FIXME write the macro def // let mut hasher = DefaultHasher::new(); // data.callee_span.hash(&mut hasher); @@ -996,7 +1007,6 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { // }.lower(self.tcx)); // } // } - self.dumper.macro_use(data); } fn process_trait_item(&mut self, trait_item: &'l ast::TraitItem, trait_id: DefId) { From e1206c4b6711dc9c252a154c21b951243bf7944b Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 28 Jul 2017 15:09:12 +1200 Subject: [PATCH 2/4] save-anlaysis: fix filter_generated --- src/librustc_save_analysis/span_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_save_analysis/span_utils.rs b/src/librustc_save_analysis/span_utils.rs index 77cde33e962..660fe7dfa3d 100644 --- a/src/librustc_save_analysis/span_utils.rs +++ b/src/librustc_save_analysis/span_utils.rs @@ -409,7 +409,7 @@ impl<'a> SpanUtils<'a> { // Otherwise, a generated span is deemed invalid if it is not a sub-span of the root // callsite. This filters out macro internal variables and most malformed spans. - !parent.source_callsite().contains(parent) + !parent.source_callsite().contains(sub_span.unwrap()) } } From bbc00c9e9c162fffddf7e8a4585aab10ecfc9d6d Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 28 Jul 2017 16:42:39 +1200 Subject: [PATCH 3/4] format!: use a dummy span rather than callee span for the span base for temporary variables --- src/libsyntax_ext/format.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index 7351377e771..9734bb867f1 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -20,7 +20,7 @@ use syntax::ext::build::AstBuilder; use syntax::parse::token; use syntax::ptr::P; use syntax::symbol::{Symbol, keywords}; -use syntax_pos::Span; +use syntax_pos::{Span, DUMMY_SP}; use syntax::tokenstream; use std::collections::{HashMap, HashSet}; @@ -558,8 +558,10 @@ impl<'a, 'b> Context<'a, 'b> { // passed to this function. for (i, e) in self.args.into_iter().enumerate() { let name = self.ecx.ident_of(&format!("__arg{}", i)); - let span = - Span { ctxt: e.span.ctxt.apply_mark(self.ecx.current_expansion.mark), ..e.span }; + let span = Span { + ctxt: e.span.ctxt.apply_mark(self.ecx.current_expansion.mark), + ..DUMMY_SP + }; pats.push(self.ecx.pat_ident(span, name)); for ref arg_ty in self.arg_unique_types[i].iter() { locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, name)); From 27b9182d5bc07d87a34c3a1ffda99d54ca2fec69 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 1 Aug 2017 14:43:11 +1200 Subject: [PATCH 4/4] review changes --- src/Cargo.lock | 1 + src/librustc_save_analysis/Cargo.toml | 1 + src/librustc_save_analysis/dump_visitor.rs | 6 +++--- src/librustc_save_analysis/lib.rs | 1 + src/librustc_save_analysis/span_utils.rs | 9 +++++---- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index fba32d4e40a..ed8a83e9e6e 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -1549,6 +1549,7 @@ dependencies = [ "rls-span 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc 0.0.0", "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_data_structures 0.0.0", "rustc_typeck 0.0.0", "syntax 0.0.0", "syntax_pos 0.0.0", diff --git a/src/librustc_save_analysis/Cargo.toml b/src/librustc_save_analysis/Cargo.toml index 2a51bf9430e..00b01994eb8 100644 --- a/src/librustc_save_analysis/Cargo.toml +++ b/src/librustc_save_analysis/Cargo.toml @@ -11,6 +11,7 @@ crate-type = ["dylib"] [dependencies] log = "0.3" rustc = { path = "../librustc" } +rustc_data_structures = { path = "../librustc_data_structures" } rustc_typeck = { path = "../librustc_typeck" } syntax = { path = "../libsyntax" } syntax_pos = { path = "../libsyntax_pos" } diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index ca27bd76fff..f74e8cb2160 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -29,8 +29,8 @@ use rustc::hir::def_id::DefId; use rustc::hir::map::Node; use rustc::session::Session; use rustc::ty::{self, TyCtxt}; +use rustc_data_structures::fx::FxHashSet; -use std::collections::HashSet; use std::path::Path; use syntax::ast::{self, NodeId, PatKind, Attribute, CRATE_NODE_ID}; @@ -75,7 +75,7 @@ pub struct DumpVisitor<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> { // we only write one macro def per unique macro definition, and // one macro use per unique callsite span. // mac_defs: HashSet, - macro_calls: HashSet, + macro_calls: FxHashSet, } impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { @@ -91,7 +91,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { span: span_utils.clone(), cur_scope: CRATE_NODE_ID, // mac_defs: HashSet::new(), - macro_calls: HashSet::new(), + macro_calls: FxHashSet(), } } diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index c9489aac981..1dd0df4108f 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -23,6 +23,7 @@ #[macro_use] extern crate log; #[macro_use] extern crate syntax; +extern crate rustc_data_structures; extern crate rustc_serialize; extern crate rustc_typeck; extern crate syntax_pos; diff --git a/src/librustc_save_analysis/span_utils.rs b/src/librustc_save_analysis/span_utils.rs index 660fe7dfa3d..e771da2ed4c 100644 --- a/src/librustc_save_analysis/span_utils.rs +++ b/src/librustc_save_analysis/span_utils.rs @@ -398,9 +398,10 @@ impl<'a> SpanUtils<'a> { return false; } // If sub_span is none, filter out generated code. - if sub_span.is_none() { - return true; - } + let sub_span = match sub_span { + Some(ss) => ss, + None => return true, + }; //If the span comes from a fake filemap, filter it. if !self.sess.codemap().lookup_char_pos(parent.lo).file.is_real_file() { @@ -409,7 +410,7 @@ impl<'a> SpanUtils<'a> { // Otherwise, a generated span is deemed invalid if it is not a sub-span of the root // callsite. This filters out macro internal variables and most malformed spans. - !parent.source_callsite().contains(sub_span.unwrap()) + !parent.source_callsite().contains(sub_span) } }