Skip to content

Commit

Permalink
Move remove_docs_from_attrs into lowering step
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb committed Mar 2, 2017
1 parent 346aed2 commit 5bfa0f3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 41 deletions.
17 changes: 8 additions & 9 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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));
}

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

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

Expand Down Expand Up @@ -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));
}
}
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -821,7 +820,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));
}

Expand Down Expand Up @@ -1330,7 +1329,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));
}

Expand Down
38 changes: 20 additions & 18 deletions src/librustc_save_analysis/external_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()
}
}

Expand Down
22 changes: 8 additions & 14 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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, _) => {
Expand All @@ -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, _) => {
Expand Down Expand Up @@ -318,7 +318,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
Expand Down Expand Up @@ -354,7 +354,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,
Expand All @@ -380,7 +380,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,
Expand Down Expand Up @@ -843,12 +843,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,
Expand Down

0 comments on commit 5bfa0f3

Please sign in to comment.