Skip to content

Commit 4e79794

Browse files
authored
transpile: add --reorganize-definitions back (#1454)
This is split off from #1451, but doesn't enable `--reorganize-definitions` by default, so it shouldn't need the fixes in #1452 yet, of which the disabling caching of zero values needs to be fixed. Also, I'm not sure if we should enable `--reorganize-definitions` by default, since `c2rust-refactor` won't be installed by default.
2 parents f1ba567 + eae4cac commit 4e79794

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

c2rust-refactor/src/bin/c2rust-refactor.rs renamed to c2rust-refactor/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::str::FromStr;
88
use c2rust_refactor::{file_io, CargoTarget, Command, Cursor, Mark, Options, RustcArgSource};
99

1010
fn main() {
11-
let yaml = load_yaml!("../refactor.yaml");
11+
let yaml = load_yaml!("refactor.yaml");
1212
let args = App::from_yaml(yaml).get_matches();
1313

1414
let opts = match parse_opts(&args) {

c2rust-transpile/src/lib.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ pub mod with_stmts;
1414

1515
use std::collections::HashSet;
1616
use std::fs::{self, File};
17-
use std::io;
1817
use std::io::prelude::*;
1918
use std::path::{Path, PathBuf};
20-
use std::process;
19+
use std::process::Command;
20+
use std::{env, io};
2121

2222
use crate::compile_cmds::CompileCmd;
2323
use failure::Error;
@@ -445,7 +445,7 @@ fn get_extra_args_macos() -> Vec<String> {
445445
if cfg!(target_os = "macos") {
446446
let usr_incl = Path::new("/usr/include");
447447
if !usr_incl.exists() {
448-
let output = process::Command::new("xcrun")
448+
let output = Command::new("xcrun")
449449
.args(["--show-sdk-path"])
450450
.output()
451451
.expect("failed to run `xcrun` subcommand");
@@ -464,8 +464,45 @@ fn get_extra_args_macos() -> Vec<String> {
464464
args
465465
}
466466

467-
fn invoke_refactor(_build_dir: &Path) -> Result<(), Error> {
468-
Ok(())
467+
fn invoke_refactor(build_dir: &Path) -> Result<(), Error> {
468+
// Make sure the crate builds cleanly
469+
let status = Command::new("cargo")
470+
.args(["check"])
471+
.env("RUSTFLAGS", "-Awarnings")
472+
.current_dir(build_dir)
473+
.status()?;
474+
if !status.success() {
475+
return Err(failure::format_err!("Crate does not compile."));
476+
}
477+
478+
// Assumes the subcommand executable is in the same directory as this program.
479+
let refactor = env::current_exe()
480+
.expect("Cannot get current executable path")
481+
.with_file_name("c2rust-refactor");
482+
let args = [
483+
"--cargo",
484+
"--rewrite-mode",
485+
"inplace",
486+
"rename_unnamed",
487+
";",
488+
"reorganize_definitions",
489+
];
490+
let status = Command::new(&refactor)
491+
.args(args)
492+
.current_dir(build_dir)
493+
.status()
494+
.map_err(|e| {
495+
let refactor = refactor.display();
496+
failure::format_err!("unable to run {refactor}: {e}\nNote that c2rust-refactor must be installed separately from c2rust and c2rust-transpile.")
497+
})?;
498+
if status.success() {
499+
Ok(())
500+
} else {
501+
Err(failure::format_err!(
502+
"Refactoring failed. Please fix errors above and re-run:\n c2rust refactor {}",
503+
args.join(" "),
504+
))
505+
}
469506
}
470507

471508
fn reorganize_definitions(
@@ -480,7 +517,7 @@ fn reorganize_definitions(
480517

481518
invoke_refactor(build_dir)?;
482519
// fix the formatting of the output of `c2rust-refactor`
483-
let status = process::Command::new("cargo")
520+
let status = Command::new("cargo")
484521
.args(["fmt"])
485522
.current_dir(build_dir)
486523
.status()?;

c2rust/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl SubCommand {
6060
/// Get all known [`SubCommand`]s. These have no [`SubCommand::path`].
6161
/// Even if the subcommand executables aren't there, we can still suggest them.
6262
pub fn known() -> impl Iterator<Item = Self> {
63-
["transpile", "instrument", "pdg", "analyze"]
63+
["transpile", "refactor", "instrument", "pdg", "analyze"]
6464
.into_iter()
6565
.map(|name| Self {
6666
path: None,

0 commit comments

Comments
 (0)