Skip to content
Open
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
2 changes: 1 addition & 1 deletion cranelift/codegen/meta/src/gen_inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ fn gen_type_constraints(all_inst: &AllInstructions, fmt: &mut Formatter) {
.collect::<Vec<_>>()
.join(", ")));
if let Some(poly) = &inst.polymorphic_info {
fmt.comment(format!("Polymorphic over {}", typeset_to_string(poly.ctrl_typevar.get_raw_typeset())));
fmt.comment(format_args!("Polymorphic over {}", typeset_to_string(poly.ctrl_typevar.get_raw_typeset())));
}

// Compute the bit field encoding, c.f. instructions.rs.
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/meta/src/gen_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ fn gen_descriptors(group: &SettingGroup, fmt: &mut Formatter) {
);
fmt.indent(|fmt| {
for preset in &group.presets {
fmt.comment(format!(
fmt.comment(format_args!(
"{}: {}",
preset.name,
preset.setting_names(group).collect::<Vec<_>>().join(", ")
Expand Down
39 changes: 23 additions & 16 deletions cranelift/srcgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::cmp;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Display;
use std::fs;
use std::io::Write;

Expand Down Expand Up @@ -43,11 +44,11 @@ impl core::fmt::Display for FileLocation {
#[macro_export]
macro_rules! fmtln {
($fmt:ident, $fmtstring:expr, $($fmtargs:expr),*) => {
$fmt.line_with_location(format!($fmtstring, $($fmtargs),*), $crate::loc!())
$fmt.line_with_location(format_args!($fmtstring, $($fmtargs),*), $crate::loc!())
};

($fmt:ident, $arg:expr) => {
$fmt.line_with_location(format!($arg), $crate::loc!())
$fmt.line_with_location(format_args!($arg), $crate::loc!())
};

($_:tt, $($args:expr),+) => {
Expand Down Expand Up @@ -131,23 +132,28 @@ impl Formatter {
}

/// Add an indented line.
pub fn line(&mut self, contents: impl AsRef<str>) {
let indented_line = format!("{}{}\n", self.get_indent(), contents.as_ref());
pub fn line(&mut self, contents: impl Display) {
let indent = self.get_indent();
let indented_line = format!("{indent}{contents}\n");
self.lines.push(indented_line);
}

/// Add an indented lin with a given a `location` appended as a comment to
/// Add an indented line with a given a `location` appended as a comment to
/// the line (this is useful for identifying where a line was generated).
pub fn line_with_location(&mut self, contents: impl AsRef<str>, location: FileLocation) {
pub fn line_with_location(&mut self, contents: impl Display, location: FileLocation) {
use std::fmt::Write;

let indent = self.get_indent();
let contents = contents.as_ref();
let indented_line = if self.lang.should_append_location(contents) {
let mut buf = String::new();

write!(&mut buf, "{indent}{contents}").unwrap();
// just after writing contents, check ends_with using should_append_location
if self.lang.should_append_location(&buf) {
let comment_token = self.lang.comment_token();
format!("{indent}{contents} {comment_token} {location}\n")
} else {
format!("{indent}{contents}\n")
};
self.lines.push(indented_line);
write!(&mut buf, " {comment_token} {location}").unwrap();
}
buf.push('\n');
self.lines.push(buf);
}

/// Pushes an empty line.
Expand All @@ -161,10 +167,11 @@ impl Formatter {
}

/// Add a comment line.
pub fn comment(&mut self, s: impl AsRef<str>) {
pub fn comment(&mut self, comment: impl Display) {
// Avoid `fmtln!` here: we don't want to append a location comment to a
// comment.
self.line(format!("{} {}", self.lang.comment_token(), s.as_ref()));
let comment_token = self.lang.comment_token();
self.line(format_args!("{comment_token} {comment}"));
}

/// Add a (multi-line) documentation comment.
Expand All @@ -186,7 +193,7 @@ impl Formatter {
/// <f()> }`. This properly indents the contents of the block.
pub fn add_block<T, F: FnOnce(&mut Formatter) -> T>(&mut self, start: &str, f: F) -> T {
assert!(matches!(self.lang, Language::Rust));
self.line(format!("{start} {{"));
self.line(format_args!("{start} {{"));
let ret = self.indent(f);
self.line("}");
ret
Expand Down
Loading