From db35604792fb64efa0a3deaebc0ea0842e19c67a Mon Sep 17 00:00:00 2001 From: Jonas Bushart Date: Thu, 2 Mar 2017 22:38:57 +0100 Subject: [PATCH] Move remove_docs_from_attrs into lowering step --- src/librustc_save_analysis/dump_visitor.rs | 17 +++++---- src/librustc_save_analysis/external_data.rs | 38 +++++++++++---------- src/librustc_save_analysis/lib.rs | 22 +++++------- 3 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index 54069775418..cbb1a3e5023 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -47,8 +47,7 @@ use syntax::ptr::P; use syntax::codemap::Spanned; use syntax_pos::*; -use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs, - remove_docs_from_attrs}; +use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs}; use super::data::*; use super::dump::Dump; use super::external_data::{Lower, make_def_id}; @@ -450,7 +449,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> { visibility: vis, docs: docs_for_attrs(attrs), sig: method_data.sig, - attributes: remove_docs_from_attrs(attrs), + attributes: attrs.to_vec(), }.lower(self.tcx)); } @@ -596,7 +595,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> { visibility: vis, docs: docs_for_attrs(attrs), sig: None, - attributes: remove_docs_from_attrs(attrs), + attributes: attrs.to_vec(), }.lower(self.tcx)); } @@ -641,7 +640,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> { visibility: From::from(&item.vis), docs: docs_for_attrs(&item.attrs), sig: self.save_ctxt.sig_base(item), - attributes: remove_docs_from_attrs(&item.attrs), + attributes: item.attrs.clone(), }.lower(self.tcx)); } @@ -707,7 +706,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> { parent: Some(make_def_id(item.id, &self.tcx.hir)), docs: docs_for_attrs(&variant.node.attrs), sig: sig, - attributes: remove_docs_from_attrs(&variant.node.attrs), + attributes: variant.node.attrs.clone(), }.lower(self.tcx)); } } @@ -734,7 +733,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> { parent: Some(make_def_id(item.id, &self.tcx.hir)), docs: docs_for_attrs(&variant.node.attrs), sig: sig, - attributes: remove_docs_from_attrs(&variant.node.attrs), + attributes: variant.node.attrs.clone(), }.lower(self.tcx)); } } @@ -806,7 +805,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> { visibility: From::from(&item.vis), docs: docs_for_attrs(&item.attrs), sig: self.save_ctxt.sig_base(item), - attributes: remove_docs_from_attrs(&item.attrs), + attributes: item.attrs.clone(), }.lower(self.tcx)); } @@ -1315,7 +1314,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, parent: None, docs: docs_for_attrs(&item.attrs), sig: Some(self.save_ctxt.sig_base(item)), - attributes: remove_docs_from_attrs(&item.attrs), + attributes: item.attrs.clone(), }.lower(self.tcx)); } diff --git a/src/librustc_save_analysis/external_data.rs b/src/librustc_save_analysis/external_data.rs index 38d1df2abb8..41658dc5b1b 100644 --- a/src/librustc_save_analysis/external_data.rs +++ b/src/librustc_save_analysis/external_data.rs @@ -14,6 +14,7 @@ use rustc::ty::TyCtxt; use syntax::ast::{self, NodeId}; use syntax::codemap::CodeMap; use syntax::print::pprust; +use syntax::symbol::Symbol; use syntax_pos::Span; use data::{self, Visibility, SigElement}; @@ -72,28 +73,29 @@ pub struct Attribute { span: SpanData, } -impl Lower for ast::Attribute { - type Target = Attribute; - - fn lower(mut self, tcx: TyCtxt) -> Attribute { - // strip #[] and #![] from the original attributes - self.style = ast::AttrStyle::Outer; - let value = pprust::attribute_to_string(&self); - // #[] are all ASCII which makes this slice save - let value = value[2..value.len()-1].to_string(); - - Attribute { - value: value, - span: SpanData::from_span(self.span, tcx.sess.codemap()), - } - } -} - impl Lower for Vec { type Target = Vec; fn lower(self, tcx: TyCtxt) -> Vec { - self.into_iter().map(|x| x.lower(tcx)).collect() + let doc = Symbol::intern("doc"); + self.into_iter() + // Only retain real attributes. Doc comments are lowered separately. + .filter(|attr| attr.name() != doc) + .map(|mut attr| { + // Remove the surrounding '#[..]' or '#![..]' of the pretty printed + // attribute. First normalize all inner attribute (#![..]) to outer + // ones (#[..]), then remove the two leading and the one trailing character. + attr.style = ast::AttrStyle::Outer; + let value = pprust::attribute_to_string(&attr); + // This str slicing works correctly, because the leading and trailing characters + // are in the ASCII range and thus exactly one byte each. + let value = value[2..value.len()-1].to_string(); + + Attribute { + value: value, + span: SpanData::from_span(attr.span, tcx.sess.codemap()), + } + }).collect() } } diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index b650fe1024b..2153b30b62c 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -136,7 +136,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { parent: None, docs: docs_for_attrs(&item.attrs), sig: self.sig_base(item), - attributes: remove_docs_from_attrs(&item.attrs), + attributes: item.attrs.clone(), })) } ast::ItemKind::Static(ref typ, mt, ref expr) => { @@ -165,7 +165,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { visibility: From::from(&item.vis), docs: docs_for_attrs(&item.attrs), sig: Some(self.sig_base(item)), - attributes: remove_docs_from_attrs(&item.attrs), + attributes: item.attrs.clone(), })) } ast::ItemKind::Const(ref typ, ref expr) => { @@ -185,7 +185,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { visibility: From::from(&item.vis), docs: docs_for_attrs(&item.attrs), sig: Some(self.sig_base(item)), - attributes: remove_docs_from_attrs(&item.attrs), + attributes: item.attrs.clone(), })) } ast::ItemKind::Mod(ref m) => { @@ -208,7 +208,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { visibility: From::from(&item.vis), docs: docs_for_attrs(&item.attrs), sig: self.sig_base(item), - attributes: remove_docs_from_attrs(&item.attrs), + attributes: item.attrs.clone(), })) } ast::ItemKind::Enum(ref def, _) => { @@ -232,7 +232,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { visibility: From::from(&item.vis), docs: docs_for_attrs(&item.attrs), sig: self.sig_base(item), - attributes: remove_docs_from_attrs(&item.attrs), + attributes: item.attrs.clone(), })) } ast::ItemKind::Impl(.., ref trait_ref, ref typ, _) => { @@ -320,7 +320,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { visibility: From::from(&field.vis), docs: docs_for_attrs(&field.attrs), sig: Some(sig), - attributes: remove_docs_from_attrs(&field.attrs), + attributes: field.attrs.clone(), }) } else { None @@ -356,7 +356,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { (result, trait_id, decl_id, From::from(&item.vis), docs_for_attrs(&item.attrs), - remove_docs_from_attrs(&item.attrs)) + item.attrs.to_vec()) } _ => { span_bug!(span, @@ -382,7 +382,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { Some(def_id), None, From::from(&item.vis), docs_for_attrs(&item.attrs), - remove_docs_from_attrs(&item.attrs)) + item.attrs.to_vec()) } r => { span_bug!(span, @@ -845,12 +845,6 @@ fn docs_for_attrs(attrs: &[Attribute]) -> String { result } -/// Remove all attributes which are docs -fn remove_docs_from_attrs(attrs: &[Attribute]) -> Vec { - let doc = Symbol::intern("doc"); - attrs.iter().cloned().filter(|attr| attr.name() != doc).collect() -} - #[derive(Clone, Copy, Debug, RustcEncodable)] pub enum Format { Csv,