Skip to content

Commit 40a12c6

Browse files
authored
transpile: Run rustfmt on generated .rs files (#1458)
- Fixes #742. Rather than running `cargo fmt`, this runs the `rustfmt` binary directly so that it can be used on individual files, and also when there are no build files.
2 parents 9e29621 + de19be8 commit 40a12c6

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

c2rust-transpile/src/build_files/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use serde_json::json;
1111

1212
use super::compile_cmds::LinkCmd;
1313
use super::TranspilerConfig;
14-
use crate::get_module_name;
1514
use crate::CrateSet;
1615
use crate::ExternCrateDetails;
1716
use crate::PragmaSet;
17+
use crate::{get_module_name, rustfmt};
1818

1919
#[derive(Debug, Copy, Clone)]
2020
pub enum BuildDirectoryContents {
@@ -225,7 +225,13 @@ fn emit_build_rs(
225225
});
226226
let output = reg.render("build.rs", &json).unwrap();
227227
let output_path = build_dir.join("build.rs");
228-
maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)
228+
let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?;
229+
230+
if !tcfg.disable_rustfmt {
231+
rustfmt(&output_path, build_dir);
232+
}
233+
234+
Some(path)
229235
}
230236

231237
/// Emit lib.rs (main.rs) for a library (binary). Returns `Some(path)`
@@ -252,8 +258,13 @@ fn emit_lib_rs(
252258

253259
let output_path = build_dir.join(file_name);
254260
let output = reg.render("lib.rs", &json).unwrap();
261+
let path = maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)?;
262+
263+
if !tcfg.disable_rustfmt {
264+
rustfmt(&output_path, build_dir);
265+
}
255266

256-
maybe_write_to_file(&output_path, output, tcfg.overwrite_existing)
267+
Some(path)
257268
}
258269

259270
/// If we translate variadic functions, the output will only compile

c2rust-transpile/src/lib.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub struct TranspilerConfig {
9797
pub output_dir: Option<PathBuf>,
9898
pub translate_const_macros: TranslateMacros,
9999
pub translate_fn_macros: TranslateMacros,
100+
pub disable_rustfmt: bool,
100101
pub disable_refactoring: bool,
101102
pub preserve_unused_functions: bool,
102103
pub log_level: log::LevelFilter,
@@ -517,14 +518,18 @@ fn reorganize_definitions(
517518
}
518519

519520
invoke_refactor(build_dir)?;
520-
// fix the formatting of the output of `c2rust-refactor`
521-
let status = Command::new("cargo")
522-
.args(["fmt"])
523-
.current_dir(build_dir)
524-
.status()?;
525-
if !status.success() {
526-
warn!("cargo fmt failed, code may not be well-formatted");
521+
522+
if !tcfg.disable_rustfmt {
523+
// fix the formatting of the output of `c2rust-refactor`
524+
let status = Command::new("cargo")
525+
.args(["fmt"])
526+
.current_dir(build_dir)
527+
.status()?;
528+
if !status.success() {
529+
warn!("cargo fmt failed, code may not be well-formatted");
530+
}
527531
}
532+
528533
Ok(())
529534
}
530535

@@ -642,6 +647,10 @@ fn transpile_single(
642647
),
643648
};
644649

650+
if !tcfg.disable_rustfmt {
651+
rustfmt(&output_path, build_dir);
652+
}
653+
645654
Ok((output_path, pragmas, crates))
646655
}
647656

@@ -689,3 +698,17 @@ fn get_output_path(
689698
input_path
690699
}
691700
}
701+
702+
fn rustfmt(output_path: &Path, build_dir: &Path) {
703+
let edition = "2021";
704+
705+
let status = Command::new("rustfmt")
706+
.args(["--edition", edition])
707+
.arg(output_path)
708+
.current_dir(build_dir)
709+
.status();
710+
711+
if !status.map_or(false, |status| status.success()) {
712+
warn!("rustfmt failed, code may not be well-formatted");
713+
}
714+
}

c2rust-transpile/tests/snapshots.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fn config() -> TranspilerConfig {
3939
output_dir: None,
4040
translate_const_macros: Default::default(),
4141
translate_fn_macros: Default::default(),
42+
disable_rustfmt: false,
4243
disable_refactoring: false,
4344
preserve_unused_functions: false,
4445
log_level: log::LevelFilter::Warn,
@@ -91,12 +92,6 @@ fn transpile(platform: Option<&str>, c_path: &Path) {
9192

9293
let edition = "2021";
9394

94-
let status = Command::new("rustfmt")
95-
.args(["--edition", edition])
96-
.arg(&rs_path)
97-
.status();
98-
assert!(status.unwrap().success());
99-
10095
let rs = fs::read_to_string(&rs_path).unwrap();
10196
let debug_expr = format!("cat {}", rs_path.display());
10297

c2rust/src/bin/c2rust-transpile.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ struct Args {
145145
#[clap(long)]
146146
emit_no_std: bool,
147147

148+
/// Disable running rustfmt after translation
149+
#[clap(long)]
150+
disable_rustfmt: bool,
151+
148152
/// Disable running refactoring tool after translation
149153
#[clap(long)]
150154
disable_refactoring: bool,
@@ -232,6 +236,7 @@ fn main() {
232236

233237
translate_const_macros: args.translate_const_macros.into(),
234238
translate_fn_macros: args.translate_fn_macros.into(),
239+
disable_rustfmt: args.disable_rustfmt,
235240
disable_refactoring: args.disable_refactoring,
236241
preserve_unused_functions: args.preserve_unused_functions,
237242

0 commit comments

Comments
 (0)