Move remove_docs_from_attrs into lowering step

This commit is contained in:
Jonas Bushart 2017-03-02 22:38:57 +01:00 committed by Alex Crichton
parent 203d22762d
commit db35604792
3 changed files with 36 additions and 41 deletions

View File

@ -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));
}

View File

@ -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<ast::Attribute> {
type Target = Vec<Attribute>;
fn lower(self, tcx: TyCtxt) -> Vec<Attribute> {
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()
}
}

View File

@ -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<Attribute> {
let doc = Symbol::intern("doc");
attrs.iter().cloned().filter(|attr| attr.name() != doc).collect()
}
#[derive(Clone, Copy, Debug, RustcEncodable)]
pub enum Format {
Csv,