Skip to content

Commit db35604

Browse files
jonasbbalexcrichton
authored andcommitted
Move remove_docs_from_attrs into lowering step
1 parent 203d227 commit db35604

File tree

3 files changed

+36
-41
lines changed

3 files changed

+36
-41
lines changed

src/librustc_save_analysis/dump_visitor.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ use syntax::ptr::P;
4747
use syntax::codemap::Spanned;
4848
use syntax_pos::*;
4949

50-
use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs,
51-
remove_docs_from_attrs};
50+
use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs};
5251
use super::data::*;
5352
use super::dump::Dump;
5453
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> {
450449
visibility: vis,
451450
docs: docs_for_attrs(attrs),
452451
sig: method_data.sig,
453-
attributes: remove_docs_from_attrs(attrs),
452+
attributes: attrs.to_vec(),
454453
}.lower(self.tcx));
455454
}
456455

@@ -596,7 +595,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
596595
visibility: vis,
597596
docs: docs_for_attrs(attrs),
598597
sig: None,
599-
attributes: remove_docs_from_attrs(attrs),
598+
attributes: attrs.to_vec(),
600599
}.lower(self.tcx));
601600
}
602601

@@ -641,7 +640,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
641640
visibility: From::from(&item.vis),
642641
docs: docs_for_attrs(&item.attrs),
643642
sig: self.save_ctxt.sig_base(item),
644-
attributes: remove_docs_from_attrs(&item.attrs),
643+
attributes: item.attrs.clone(),
645644
}.lower(self.tcx));
646645
}
647646

@@ -707,7 +706,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
707706
parent: Some(make_def_id(item.id, &self.tcx.hir)),
708707
docs: docs_for_attrs(&variant.node.attrs),
709708
sig: sig,
710-
attributes: remove_docs_from_attrs(&variant.node.attrs),
709+
attributes: variant.node.attrs.clone(),
711710
}.lower(self.tcx));
712711
}
713712
}
@@ -734,7 +733,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
734733
parent: Some(make_def_id(item.id, &self.tcx.hir)),
735734
docs: docs_for_attrs(&variant.node.attrs),
736735
sig: sig,
737-
attributes: remove_docs_from_attrs(&variant.node.attrs),
736+
attributes: variant.node.attrs.clone(),
738737
}.lower(self.tcx));
739738
}
740739
}
@@ -806,7 +805,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
806805
visibility: From::from(&item.vis),
807806
docs: docs_for_attrs(&item.attrs),
808807
sig: self.save_ctxt.sig_base(item),
809-
attributes: remove_docs_from_attrs(&item.attrs),
808+
attributes: item.attrs.clone(),
810809
}.lower(self.tcx));
811810
}
812811

@@ -1315,7 +1314,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
13151314
parent: None,
13161315
docs: docs_for_attrs(&item.attrs),
13171316
sig: Some(self.save_ctxt.sig_base(item)),
1318-
attributes: remove_docs_from_attrs(&item.attrs),
1317+
attributes: item.attrs.clone(),
13191318
}.lower(self.tcx));
13201319
}
13211320

src/librustc_save_analysis/external_data.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc::ty::TyCtxt;
1414
use syntax::ast::{self, NodeId};
1515
use syntax::codemap::CodeMap;
1616
use syntax::print::pprust;
17+
use syntax::symbol::Symbol;
1718
use syntax_pos::Span;
1819

1920
use data::{self, Visibility, SigElement};
@@ -72,28 +73,29 @@ pub struct Attribute {
7273
span: SpanData,
7374
}
7475

75-
impl Lower for ast::Attribute {
76-
type Target = Attribute;
77-
78-
fn lower(mut self, tcx: TyCtxt) -> Attribute {
79-
// strip #[] and #![] from the original attributes
80-
self.style = ast::AttrStyle::Outer;
81-
let value = pprust::attribute_to_string(&self);
82-
// #[] are all ASCII which makes this slice save
83-
let value = value[2..value.len()-1].to_string();
84-
85-
Attribute {
86-
value: value,
87-
span: SpanData::from_span(self.span, tcx.sess.codemap()),
88-
}
89-
}
90-
}
91-
9276
impl Lower for Vec<ast::Attribute> {
9377
type Target = Vec<Attribute>;
9478

9579
fn lower(self, tcx: TyCtxt) -> Vec<Attribute> {
96-
self.into_iter().map(|x| x.lower(tcx)).collect()
80+
let doc = Symbol::intern("doc");
81+
self.into_iter()
82+
// Only retain real attributes. Doc comments are lowered separately.
83+
.filter(|attr| attr.name() != doc)
84+
.map(|mut attr| {
85+
// Remove the surrounding '#[..]' or '#![..]' of the pretty printed
86+
// attribute. First normalize all inner attribute (#![..]) to outer
87+
// ones (#[..]), then remove the two leading and the one trailing character.
88+
attr.style = ast::AttrStyle::Outer;
89+
let value = pprust::attribute_to_string(&attr);
90+
// This str slicing works correctly, because the leading and trailing characters
91+
// are in the ASCII range and thus exactly one byte each.
92+
let value = value[2..value.len()-1].to_string();
93+
94+
Attribute {
95+
value: value,
96+
span: SpanData::from_span(attr.span, tcx.sess.codemap()),
97+
}
98+
}).collect()
9799
}
98100
}
99101

src/librustc_save_analysis/lib.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
136136
parent: None,
137137
docs: docs_for_attrs(&item.attrs),
138138
sig: self.sig_base(item),
139-
attributes: remove_docs_from_attrs(&item.attrs),
139+
attributes: item.attrs.clone(),
140140
}))
141141
}
142142
ast::ItemKind::Static(ref typ, mt, ref expr) => {
@@ -165,7 +165,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
165165
visibility: From::from(&item.vis),
166166
docs: docs_for_attrs(&item.attrs),
167167
sig: Some(self.sig_base(item)),
168-
attributes: remove_docs_from_attrs(&item.attrs),
168+
attributes: item.attrs.clone(),
169169
}))
170170
}
171171
ast::ItemKind::Const(ref typ, ref expr) => {
@@ -185,7 +185,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
185185
visibility: From::from(&item.vis),
186186
docs: docs_for_attrs(&item.attrs),
187187
sig: Some(self.sig_base(item)),
188-
attributes: remove_docs_from_attrs(&item.attrs),
188+
attributes: item.attrs.clone(),
189189
}))
190190
}
191191
ast::ItemKind::Mod(ref m) => {
@@ -208,7 +208,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
208208
visibility: From::from(&item.vis),
209209
docs: docs_for_attrs(&item.attrs),
210210
sig: self.sig_base(item),
211-
attributes: remove_docs_from_attrs(&item.attrs),
211+
attributes: item.attrs.clone(),
212212
}))
213213
}
214214
ast::ItemKind::Enum(ref def, _) => {
@@ -232,7 +232,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
232232
visibility: From::from(&item.vis),
233233
docs: docs_for_attrs(&item.attrs),
234234
sig: self.sig_base(item),
235-
attributes: remove_docs_from_attrs(&item.attrs),
235+
attributes: item.attrs.clone(),
236236
}))
237237
}
238238
ast::ItemKind::Impl(.., ref trait_ref, ref typ, _) => {
@@ -320,7 +320,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
320320
visibility: From::from(&field.vis),
321321
docs: docs_for_attrs(&field.attrs),
322322
sig: Some(sig),
323-
attributes: remove_docs_from_attrs(&field.attrs),
323+
attributes: field.attrs.clone(),
324324
})
325325
} else {
326326
None
@@ -356,7 +356,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
356356
(result, trait_id, decl_id,
357357
From::from(&item.vis),
358358
docs_for_attrs(&item.attrs),
359-
remove_docs_from_attrs(&item.attrs))
359+
item.attrs.to_vec())
360360
}
361361
_ => {
362362
span_bug!(span,
@@ -382,7 +382,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
382382
Some(def_id), None,
383383
From::from(&item.vis),
384384
docs_for_attrs(&item.attrs),
385-
remove_docs_from_attrs(&item.attrs))
385+
item.attrs.to_vec())
386386
}
387387
r => {
388388
span_bug!(span,
@@ -845,12 +845,6 @@ fn docs_for_attrs(attrs: &[Attribute]) -> String {
845845
result
846846
}
847847

848-
/// Remove all attributes which are docs
849-
fn remove_docs_from_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
850-
let doc = Symbol::intern("doc");
851-
attrs.iter().cloned().filter(|attr| attr.name() != doc).collect()
852-
}
853-
854848
#[derive(Clone, Copy, Debug, RustcEncodable)]
855849
pub enum Format {
856850
Csv,

0 commit comments

Comments
 (0)