Skip to content

Commit 0eb2e56

Browse files
Combine comment-handling logic into struct
This also permits sharing the underlying code between pprust and hir::print.
1 parent 9b5e397 commit 0eb2e56

File tree

2 files changed

+65
-71
lines changed

2 files changed

+65
-71
lines changed

src/librustc/hir/print.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use rustc_target::spec::abi::Abi;
22
use syntax::ast;
33
use syntax::source_map::{SourceMap, Spanned};
44
use syntax::parse::ParseSess;
5-
use syntax::parse::lexer::comments;
65
use syntax::print::pp::{self, Breaks};
76
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
8-
use syntax::print::pprust::PrintState;
7+
use syntax::print::pprust::{Comments, PrintState};
98
use syntax::symbol::kw;
109
use syntax::util::parser::{self, AssocOp, Fixity};
1110
use syntax_pos::{self, BytePos, FileName};
@@ -71,9 +70,7 @@ impl PpAnn for hir::Crate {
7170

7271
pub struct State<'a> {
7372
pub s: pp::Printer<'a>,
74-
cm: Option<&'a SourceMap>,
75-
comments: Vec<comments::Comment>,
76-
cur_cmnt: usize,
73+
comments: Option<Comments<'a>>,
7774
ann: &'a (dyn PpAnn + 'a),
7875
}
7976

@@ -82,13 +79,9 @@ impl<'a> PrintState<'a> for State<'a> {
8279
&mut self.s
8380
}
8481

85-
fn comments(&mut self) -> &mut Vec<comments::Comment> {
82+
fn comments(&mut self) -> &mut Option<Comments<'a>> {
8683
&mut self.comments
8784
}
88-
89-
fn cur_cmnt(&mut self) -> &mut usize {
90-
&mut self.cur_cmnt
91-
}
9285
}
9386

9487
#[allow(non_upper_case_globals)]
@@ -122,12 +115,9 @@ impl<'a> State<'a> {
122115
out: &'a mut String,
123116
ann: &'a dyn PpAnn)
124117
-> State<'a> {
125-
let comments = comments::gather_comments(sess, filename, input);
126118
State {
127119
s: pp::mk_printer(out),
128-
cm: Some(cm),
129-
comments: comments,
130-
cur_cmnt: 0,
120+
comments: Some(Comments::new(cm, sess, filename, input)),
131121
ann,
132122
}
133123
}
@@ -140,9 +130,7 @@ pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
140130
{
141131
let mut printer = State {
142132
s: pp::mk_printer(&mut wr),
143-
cm: None,
144-
comments: Vec::new(),
145-
cur_cmnt: 0,
133+
comments: None,
146134
ann,
147135
};
148136
f(&mut printer);
@@ -2151,23 +2139,9 @@ impl<'a> State<'a> {
21512139
span: syntax_pos::Span,
21522140
next_pos: Option<BytePos>)
21532141
{
2154-
let cm = match self.cm {
2155-
Some(cm) => cm,
2156-
_ => return,
2157-
};
2158-
if let Some(ref cmnt) = self.next_comment() {
2159-
if (*cmnt).style != comments::Trailing {
2160-
return;
2161-
}
2162-
let span_line = cm.lookup_char_pos(span.hi());
2163-
let comment_line = cm.lookup_char_pos((*cmnt).pos);
2164-
let mut next = (*cmnt).pos + BytePos(1);
2165-
if let Some(p) = next_pos {
2166-
next = p;
2167-
}
2168-
if span.hi() < (*cmnt).pos && (*cmnt).pos < next &&
2169-
span_line.line == comment_line.line {
2170-
self.print_comment(cmnt);
2142+
if let Some(cmnts) = self.comments() {
2143+
if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
2144+
self.print_comment(&cmnt);
21712145
}
21722146
}
21732147
}

src/libsyntax/print/pprust.rs

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,53 @@ pub struct NoAnn;
4343

4444
impl PpAnn for NoAnn {}
4545

46+
pub struct Comments<'a> {
47+
cm: &'a SourceMap,
48+
comments: Vec<comments::Comment>,
49+
current: usize,
50+
}
51+
52+
impl<'a> Comments<'a> {
53+
pub fn new(
54+
cm: &'a SourceMap,
55+
sess: &ParseSess,
56+
filename: FileName,
57+
input: String,
58+
) -> Comments<'a> {
59+
let comments = comments::gather_comments(sess, filename, input);
60+
Comments {
61+
cm,
62+
comments,
63+
current: 0,
64+
}
65+
}
66+
67+
pub fn next(&self) -> Option<comments::Comment> {
68+
self.comments.get(self.current).cloned()
69+
}
70+
71+
pub fn trailing_comment(
72+
&mut self,
73+
span: syntax_pos::Span,
74+
next_pos: Option<BytePos>,
75+
) -> Option<comments::Comment> {
76+
if let Some(cmnt) = self.next() {
77+
if cmnt.style != comments::Trailing { return None; }
78+
let span_line = self.cm.lookup_char_pos(span.hi());
79+
let comment_line = self.cm.lookup_char_pos(cmnt.pos);
80+
let next = next_pos.unwrap_or_else(|| cmnt.pos + BytePos(1));
81+
if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
82+
return Some(cmnt);
83+
}
84+
}
85+
86+
None
87+
}
88+
}
89+
4690
pub struct State<'a> {
4791
pub s: pp::Printer<'a>,
48-
cm: Option<&'a SourceMap>,
49-
comments: Vec<comments::Comment>,
50-
cur_cmnt: usize,
92+
comments: Option<Comments<'a>>,
5193
ann: &'a (dyn PpAnn+'a),
5294
is_expanded: bool
5395
}
@@ -98,12 +140,9 @@ impl<'a> State<'a> {
98140
out: &'a mut String,
99141
ann: &'a dyn PpAnn,
100142
is_expanded: bool) -> State<'a> {
101-
let comments = comments::gather_comments(sess, filename, input);
102143
State {
103144
s: pp::mk_printer(out),
104-
cm: Some(cm),
105-
comments,
106-
cur_cmnt: 0,
145+
comments: Some(Comments::new(cm, sess, filename, input)),
107146
ann,
108147
is_expanded,
109148
}
@@ -117,9 +156,7 @@ pub fn to_string<F>(f: F) -> String where
117156
{
118157
let mut printer = State {
119158
s: pp::mk_printer(&mut wr),
120-
cm: None,
121-
comments: Vec::new(),
122-
cur_cmnt: 0,
159+
comments: None,
123160
ann: &NoAnn,
124161
is_expanded: false
125162
};
@@ -415,8 +452,7 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
415452

416453
pub trait PrintState<'a> {
417454
fn writer(&mut self) -> &mut pp::Printer<'a>;
418-
fn comments(&mut self) -> &mut Vec<comments::Comment>;
419-
fn cur_cmnt(&mut self) -> &mut usize;
455+
fn comments(&mut self) -> &mut Option<Comments<'a>>;
420456

421457
fn word_space<S: Into<Cow<'static, str>>>(&mut self, w: S) {
422458
self.writer().word(w);
@@ -537,17 +573,13 @@ pub trait PrintState<'a> {
537573
self.writer().hardbreak();
538574
}
539575
}
540-
*self.cur_cmnt() = *self.cur_cmnt() + 1;
576+
if let Some(cm) = self.comments() {
577+
cm.current += 1;
578+
}
541579
}
542580

543581
fn next_comment(&mut self) -> Option<comments::Comment> {
544-
let cur_cmnt = *self.cur_cmnt();
545-
let cmnts = &*self.comments();
546-
if cur_cmnt < cmnts.len() {
547-
Some(cmnts[cur_cmnt].clone())
548-
} else {
549-
None
550-
}
582+
self.comments().as_mut().and_then(|c| c.next())
551583
}
552584

553585
fn print_literal(&mut self, lit: &ast::Lit) {
@@ -744,13 +776,9 @@ impl<'a> PrintState<'a> for State<'a> {
744776
&mut self.s
745777
}
746778

747-
fn comments(&mut self) -> &mut Vec<comments::Comment> {
779+
fn comments(&mut self) -> &mut Option<Comments<'a>> {
748780
&mut self.comments
749781
}
750-
751-
fn cur_cmnt(&mut self) -> &mut usize {
752-
&mut self.cur_cmnt
753-
}
754782
}
755783

756784
impl<'a> State<'a> {
@@ -2913,18 +2941,10 @@ impl<'a> State<'a> {
29132941

29142942
crate fn maybe_print_trailing_comment(&mut self, span: syntax_pos::Span,
29152943
next_pos: Option<BytePos>)
2916-
{
2917-
let cm = match self.cm {
2918-
Some(cm) => cm,
2919-
_ => return,
2920-
};
2921-
if let Some(ref cmnt) = self.next_comment() {
2922-
if cmnt.style != comments::Trailing { return; }
2923-
let span_line = cm.lookup_char_pos(span.hi());
2924-
let comment_line = cm.lookup_char_pos(cmnt.pos);
2925-
let next = next_pos.unwrap_or_else(|| cmnt.pos + BytePos(1));
2926-
if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
2927-
self.print_comment(cmnt);
2944+
{
2945+
if let Some(cmnts) = self.comments() {
2946+
if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
2947+
self.print_comment(&cmnt);
29282948
}
29292949
}
29302950
}

0 commit comments

Comments
 (0)