Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup to syntax::print/rustc::hir::print #62098

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::hir::{GenericParam, GenericParamKind, GenericArg};

use std::borrow::Cow;
use std::cell::Cell;
use std::io::{self, Write, Read};
use std::io::{self, Read};
use std::vec;

pub enum AnnNode<'a> {
Expand Down Expand Up @@ -112,7 +112,7 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
krate: &hir::Crate,
filename: FileName,
input: &mut dyn Read,
out: Box<dyn Write + 'a>,
out: &'a mut Vec<u8>,
ann: &'a dyn PpAnn)
-> io::Result<()> {
let mut s = State::new_from_input(cm, sess, filename, input, out, ann);
Expand All @@ -130,15 +130,15 @@ impl<'a> State<'a> {
sess: &ParseSess,
filename: FileName,
input: &mut dyn Read,
out: Box<dyn Write + 'a>,
out: &'a mut Vec<u8>,
ann: &'a dyn PpAnn)
-> State<'a> {
let comments = comments::gather_comments(sess, filename, input);
State::new(cm, out, ann, Some(comments))
}

pub fn new(cm: &'a SourceMap,
out: Box<dyn Write + 'a>,
out: &'a mut Vec<u8>,
ann: &'a dyn PpAnn,
comments: Option<Vec<comments::Comment>>)
-> State<'a> {
Expand All @@ -159,7 +159,7 @@ pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
let mut wr = Vec::new();
{
let mut printer = State {
s: pp::mk_printer(Box::new(&mut wr), default_columns),
s: pp::mk_printer(&mut wr, default_columns),
cm: None,
comments: None,
cur_cmnt: 0,
Expand Down
18 changes: 9 additions & 9 deletions src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ pub fn print_after_parsing(sess: &Session,

if let PpmSource(s) = ppm {
// Silently ignores an identified node.
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support(sess, None, move |annotation| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
Expand All @@ -742,7 +742,7 @@ pub fn print_after_parsing(sess: &Session,
krate,
src_name,
&mut rdr,
box out,
out,
annotation.pp_ann(),
false)
}).unwrap()
Expand Down Expand Up @@ -779,7 +779,7 @@ pub fn print_after_hir_lowering<'tcx>(
match (ppm, opt_uii) {
(PpmSource(s), _) => {
// Silently ignores an identified node.
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support(tcx.sess, Some(tcx), move |annotation| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
Expand All @@ -788,14 +788,14 @@ pub fn print_after_hir_lowering<'tcx>(
krate,
src_name,
&mut rdr,
box out,
out,
annotation.pp_ann(),
true)
})
}

(PpmHir(s), None) => {
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support_hir(tcx, move |annotation, krate| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
Expand All @@ -804,21 +804,21 @@ pub fn print_after_hir_lowering<'tcx>(
krate,
src_name,
&mut rdr,
box out,
out,
annotation.pp_ann())
})
}

(PpmHirTree(s), None) => {
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support_hir(tcx, move |_annotation, krate| {
debug!("pretty printing source code {:?}", s);
write!(out, "{:#?}", krate)
})
}

(PpmHir(s), Some(uii)) => {
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support_hir(tcx, move |annotation, _| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
Expand All @@ -827,7 +827,7 @@ pub fn print_after_hir_lowering<'tcx>(
&sess.parse_sess,
src_name,
&mut rdr,
box out,
out,
annotation.pp_ann());
for node_id in uii.all_matching_node_ids(hir_map) {
let hir_id = tcx.hir().node_to_hir_id(node_id);
Expand Down
66 changes: 32 additions & 34 deletions src/libsyntax/print/pp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub enum Token {
}

impl Token {
pub fn is_eof(&self) -> bool {
crate fn is_eof(&self) -> bool {
match *self {
Token::Eof => true,
_ => false,
Expand Down Expand Up @@ -223,20 +223,20 @@ fn buf_str(buf: &[BufEntry], left: usize, right: usize, lim: usize) -> String {
}

#[derive(Copy, Clone)]
pub enum PrintStackBreak {
crate enum PrintStackBreak {
Fits,
Broken(Breaks),
}

#[derive(Copy, Clone)]
pub struct PrintStackElem {
crate struct PrintStackElem {
offset: isize,
pbreak: PrintStackBreak
}

const SIZE_INFINITY: isize = 0xffff;

pub fn mk_printer<'a>(out: Box<dyn io::Write+'a>, linewidth: usize) -> Printer<'a> {
pub fn mk_printer(out: &mut Vec<u8>, linewidth: usize) -> Printer<'_> {
// Yes 55, it makes the ring buffers big enough to never fall behind.
let n: usize = 55 * linewidth;
debug!("mk_printer {}", linewidth);
Expand All @@ -259,7 +259,7 @@ pub fn mk_printer<'a>(out: Box<dyn io::Write+'a>, linewidth: usize) -> Printer<'
}

pub struct Printer<'a> {
out: Box<dyn io::Write+'a>,
out: &'a mut Vec<u8>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a String?

buf_max_len: usize,
/// Width of lines we're constrained to
margin: isize,
Expand Down Expand Up @@ -386,7 +386,7 @@ impl<'a> Printer<'a> {
}
}

pub fn check_stream(&mut self) -> io::Result<()> {
crate fn check_stream(&mut self) -> io::Result<()> {
debug!("check_stream Vec<{}, {}> with left_total={}, right_total={}",
self.left, self.right, self.left_total, self.right_total);
if self.right_total - self.left_total > self.space {
Expand All @@ -405,24 +405,24 @@ impl<'a> Printer<'a> {
Ok(())
}

pub fn scan_push(&mut self, x: usize) {
crate fn scan_push(&mut self, x: usize) {
debug!("scan_push {}", x);
self.scan_stack.push_front(x);
}

pub fn scan_pop(&mut self) -> usize {
crate fn scan_pop(&mut self) -> usize {
self.scan_stack.pop_front().unwrap()
}

pub fn scan_top(&mut self) -> usize {
crate fn scan_top(&mut self) -> usize {
*self.scan_stack.front().unwrap()
}

pub fn scan_pop_bottom(&mut self) -> usize {
crate fn scan_pop_bottom(&mut self) -> usize {
self.scan_stack.pop_back().unwrap()
}

pub fn advance_right(&mut self) {
crate fn advance_right(&mut self) {
self.right += 1;
self.right %= self.buf_max_len;
// Extend the buf if necessary.
Expand All @@ -432,7 +432,7 @@ impl<'a> Printer<'a> {
assert_ne!(self.right, self.left);
}

pub fn advance_left(&mut self) -> io::Result<()> {
crate fn advance_left(&mut self) -> io::Result<()> {
debug!("advance_left Vec<{},{}>, sizeof({})={}", self.left, self.right,
self.left, self.buf[self.left].size);

Expand Down Expand Up @@ -467,7 +467,7 @@ impl<'a> Printer<'a> {
Ok(())
}

pub fn check_stack(&mut self, k: isize) {
crate fn check_stack(&mut self, k: isize) {
if !self.scan_stack.is_empty() {
let x = self.scan_top();
match self.buf[x].token {
Expand Down Expand Up @@ -495,20 +495,20 @@ impl<'a> Printer<'a> {
}
}

pub fn print_newline(&mut self, amount: isize) -> io::Result<()> {
crate fn print_newline(&mut self, amount: isize) -> io::Result<()> {
debug!("NEWLINE {}", amount);
let ret = write!(self.out, "\n");
self.out.push(b'\n');
self.pending_indentation = 0;
self.indent(amount);
ret
Ok(())
}

pub fn indent(&mut self, amount: isize) {
crate fn indent(&mut self, amount: isize) {
debug!("INDENT {}", amount);
self.pending_indentation += amount;
}

pub fn get_top(&mut self) -> PrintStackElem {
crate fn get_top(&mut self) -> PrintStackElem {
match self.print_stack.last() {
Some(el) => *el,
None => PrintStackElem {
Expand All @@ -518,7 +518,7 @@ impl<'a> Printer<'a> {
}
}

pub fn print_begin(&mut self, b: BeginToken, l: isize) -> io::Result<()> {
crate fn print_begin(&mut self, b: BeginToken, l: isize) -> io::Result<()> {
if l > self.space {
let col = self.margin - self.space + b.offset;
debug!("print Begin -> push broken block at col {}", col);
Expand All @@ -536,15 +536,15 @@ impl<'a> Printer<'a> {
Ok(())
}

pub fn print_end(&mut self) -> io::Result<()> {
crate fn print_end(&mut self) -> io::Result<()> {
debug!("print End -> pop End");
let print_stack = &mut self.print_stack;
assert!(!print_stack.is_empty());
print_stack.pop().unwrap();
Ok(())
}

pub fn print_break(&mut self, b: BreakToken, l: isize) -> io::Result<()> {
crate fn print_break(&mut self, b: BreakToken, l: isize) -> io::Result<()> {
let top = self.get_top();
match top.pbreak {
PrintStackBreak::Fits => {
Expand Down Expand Up @@ -578,7 +578,7 @@ impl<'a> Printer<'a> {
}
}

pub fn print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
crate fn print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
debug!("print String({})", s);
// assert!(len <= space);
self.space -= len;
Expand All @@ -592,18 +592,20 @@ impl<'a> Printer<'a> {
// difference is significant on some workloads.
let spaces_len = SPACES.len() as isize;
while self.pending_indentation >= spaces_len {
self.out.write_all(&SPACES)?;
self.out.extend_from_slice(&SPACES);
self.pending_indentation -= spaces_len;
}
if self.pending_indentation > 0 {
self.out.write_all(&SPACES[0..self.pending_indentation as usize])?;
self.out.extend_from_slice(&SPACES[0..self.pending_indentation as usize]);
self.pending_indentation = 0;
}

write!(self.out, "{}", s)
self.out.extend_from_slice(s.as_bytes());

Ok(())
}

pub fn print(&mut self, token: Token, l: isize) -> io::Result<()> {
crate fn print(&mut self, token: Token, l: isize) -> io::Result<()> {
debug!("print {} {} (remaining line space={})", token, l,
self.space);
debug!("{}", buf_str(&self.buf,
Expand All @@ -625,15 +627,15 @@ impl<'a> Printer<'a> {
// Convenience functions to talk to the printer.

/// "raw box"
pub fn rbox(&mut self, indent: usize, b: Breaks) -> io::Result<()> {
crate fn rbox(&mut self, indent: usize, b: Breaks) -> io::Result<()> {
self.pretty_print_begin(BeginToken {
offset: indent as isize,
breaks: b
})
}

/// Inconsistent breaking box
pub fn ibox(&mut self, indent: usize) -> io::Result<()> {
crate fn ibox(&mut self, indent: usize) -> io::Result<()> {
self.rbox(indent, Breaks::Inconsistent)
}

Expand All @@ -649,7 +651,7 @@ impl<'a> Printer<'a> {
})
}

pub fn end(&mut self) -> io::Result<()> {
crate fn end(&mut self) -> io::Result<()> {
self.pretty_print_end()
}

Expand All @@ -667,7 +669,7 @@ impl<'a> Printer<'a> {
self.break_offset(n, 0)
}

pub fn zerobreak(&mut self) -> io::Result<()> {
crate fn zerobreak(&mut self) -> io::Result<()> {
self.spaces(0)
}

Expand All @@ -682,8 +684,4 @@ impl<'a> Printer<'a> {
pub fn hardbreak_tok_offset(off: isize) -> Token {
Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
}

pub fn hardbreak_tok() -> Token {
Self::hardbreak_tok_offset(0)
}
}
Loading