Skip to content

Commit 3ebf706

Browse files
committed
Define and use a print_maybe_styled! macro in libsyntax/diagnostic.rs
`EmitterWriter::print_maybe_styled` was basically always used with `format!`, so this macro makes some code cleaner. It should also remove some unnecessary allocations (most `print_maybe_styled` invocations allocated a `String` previously, whereas the new macro uses `write_fmt` to write the formatted string directly to the terminal). This probably could have been part of #26838, but it’s too late now.
1 parent e05ac39 commit 3ebf706

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/libsyntax/diagnostic.rs

+38-20
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,20 @@ enum Destination {
318318
Raw(Box<Write + Send>),
319319
}
320320

321+
/// Do not use this for messages that end in `\n` – use `println_maybe_styled` instead. See
322+
/// `EmitterWriter::print_maybe_styled` for details.
323+
macro_rules! print_maybe_styled {
324+
($writer: expr, $style: expr, $($arg: tt)*) => {
325+
$writer.print_maybe_styled(format_args!($($arg)*), $style, false)
326+
}
327+
}
328+
329+
macro_rules! println_maybe_styled {
330+
($writer: expr, $style: expr, $($arg: tt)*) => {
331+
$writer.print_maybe_styled(format_args!($($arg)*), $style, true)
332+
}
333+
}
334+
321335
impl EmitterWriter {
322336
pub fn stderr(color_config: ColorConfig,
323337
registry: Option<diagnostics::registry::Registry>) -> EmitterWriter {
@@ -346,8 +360,9 @@ impl EmitterWriter {
346360
}
347361

348362
fn print_maybe_styled(&mut self,
349-
msg: &str,
350-
color: term::attr::Attr) -> io::Result<()> {
363+
args: fmt::Arguments,
364+
color: term::attr::Attr,
365+
print_newline_at_end: bool) -> io::Result<()> {
351366
match self.dst {
352367
Terminal(ref mut t) => {
353368
try!(t.attr(color));
@@ -364,17 +379,22 @@ impl EmitterWriter {
364379
// once, which still leaves the opportunity for interleaved output
365380
// to be miscolored. We assume this is rare enough that we don't
366381
// have to worry about it.
367-
if msg.ends_with("\n") {
368-
try!(t.write_all(msg[..msg.len()-1].as_bytes()));
369-
try!(t.reset());
370-
try!(t.write_all(b"\n"));
382+
try!(t.write_fmt(args));
383+
try!(t.reset());
384+
if print_newline_at_end {
385+
t.write_all(b"\n")
386+
} else {
387+
Ok(())
388+
}
389+
}
390+
Raw(ref mut w) => {
391+
try!(w.write_fmt(args));
392+
if print_newline_at_end {
393+
w.write_all(b"\n")
371394
} else {
372-
try!(t.write_all(msg.as_bytes()));
373-
try!(t.reset());
395+
Ok(())
374396
}
375-
Ok(())
376397
}
377-
Raw(ref mut w) => w.write_all(msg.as_bytes()),
378398
}
379399
}
380400

@@ -384,15 +404,14 @@ impl EmitterWriter {
384404
try!(write!(&mut self.dst, "{} ", topic));
385405
}
386406

387-
try!(self.print_maybe_styled(&format!("{}: ", lvl.to_string()),
388-
term::attr::ForegroundColor(lvl.color())));
389-
try!(self.print_maybe_styled(&format!("{}", msg),
390-
term::attr::Bold));
407+
try!(print_maybe_styled!(self, term::attr::ForegroundColor(lvl.color()),
408+
"{}: ", lvl.to_string()));
409+
try!(print_maybe_styled!(self, term::attr::Bold, "{}", msg));
391410

392411
match code {
393412
Some(code) => {
394413
let style = term::attr::ForegroundColor(term::color::BRIGHT_MAGENTA);
395-
try!(self.print_maybe_styled(&format!(" [{}]", code.clone()), style));
414+
try!(print_maybe_styled!(self, style, " [{}]", code.clone()));
396415
}
397416
None => ()
398417
}
@@ -623,8 +642,8 @@ impl EmitterWriter {
623642
s.pop();
624643
}
625644

626-
try!(self.print_maybe_styled(&format!("{}\n", s),
627-
term::attr::ForegroundColor(lvl.color())));
645+
try!(println_maybe_styled!(self, term::attr::ForegroundColor(lvl.color()),
646+
"{}", s));
628647
}
629648
}
630649
Ok(())
@@ -696,9 +715,8 @@ impl EmitterWriter {
696715
}
697716
}
698717
s.push('^');
699-
s.push('\n');
700-
self.print_maybe_styled(&s[..],
701-
term::attr::ForegroundColor(lvl.color()))
718+
println_maybe_styled!(self, term::attr::ForegroundColor(lvl.color()),
719+
"{}", s)
702720
}
703721

704722
fn print_macro_backtrace(&mut self,

0 commit comments

Comments
 (0)