diff --git a/README.md b/README.md index 392f27969f..021e0ae293 100644 --- a/README.md +++ b/README.md @@ -223,7 +223,7 @@ normal Rust modules. They will export and import functions through the C API. These modules can be compiled together into a single static Rust library or binary. -You can run with `--reorganize-definitions` (which invokes `c2rust-refactor`), +You can run with `--refactor`, which should deduplicate definitions and directly import them with `use`s instead of through the C API. diff --git a/c2rust-refactor/src/transform/reorganize_definitions.rs b/c2rust-refactor/src/transform/reorganize_definitions.rs index 439a4445cd..8390d5b6c2 100644 --- a/c2rust-refactor/src/transform/reorganize_definitions.rs +++ b/c2rust-refactor/src/transform/reorganize_definitions.rs @@ -39,7 +39,7 @@ use super::externs; /// Usage: `reorganize_definitions` /// /// This refactoring operates on code transpiled with the -/// `--reorganize-definitions` flag. +/// `--emit-refactor-annotations` flag. /// /// This pass refactors a crate to de-duplicate declarations, move them into /// their relevant modules and import the items as needed, rather than using diff --git a/c2rust-transpile/src/lib.rs b/c2rust-transpile/src/lib.rs index 17238a2e3c..bceb24a804 100644 --- a/c2rust-transpile/src/lib.rs +++ b/c2rust-transpile/src/lib.rs @@ -96,19 +96,24 @@ pub struct TranspilerConfig { pub translate_valist: bool, pub overwrite_existing: bool, pub reduce_type_annotations: bool, - pub reorganize_definitions: bool, pub enabled_warnings: HashSet, pub emit_no_std: bool, pub output_dir: Option, pub translate_const_macros: TranslateMacros, pub translate_fn_macros: TranslateMacros, pub disable_rustfmt: bool, - pub disable_refactoring: bool, pub preserve_unused_functions: bool, pub log_level: log::LevelFilter, pub edition: RustEdition, - /// Run `c2rust-postprocess` after transpiling and potentially refactoring. + /// Run `c2rust-refactor` after transpiling (and before [`Self::postprocess`]). + pub refactor: bool, + + /// Emit annotations needed by the refactorer for certain transforms, such as `reorganize_definitions`. + /// Implied by [`Self::refactor`]. + pub emit_refactor_annotations: bool, + + /// Run `c2rust-postprocess` after transpiling (and after [`Self::refactor`]). pub postprocess: bool, // Options that control build files @@ -547,7 +552,7 @@ fn reorganize_definitions( crate_file: Option<&Path>, ) -> Result<(), Error> { // We only run the reorganization refactoring if we emitted a fresh crate file - if crate_file.is_none() || tcfg.disable_refactoring || !tcfg.reorganize_definitions { + if crate_file.is_none() || !tcfg.refactor { return Ok(()); } diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 6813bbf63f..3a7c3e735f 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -841,7 +841,7 @@ pub fn translate( { let export_type = |decl_id: CDeclId, decl: &CDecl| { let decl_file_id = t.ast_context.file_id(decl); - if t.tcfg.reorganize_definitions { + if t.tcfg.emit_refactor_annotations { t.cur_file.set(decl_file_id); } match t.convert_decl(ctx, decl_id) { @@ -870,7 +870,7 @@ pub fn translate( } t.cur_file.take(); - if t.tcfg.reorganize_definitions + if t.tcfg.emit_refactor_annotations && decl_file_id.map_or(false, |id| id != t.main_file) { let name = t @@ -929,7 +929,7 @@ pub fn translate( let decl = t.ast_context.get_decl(&top_id).unwrap(); let decl_file_id = t.ast_context.file_id(decl); - if t.tcfg.reorganize_definitions + if t.tcfg.emit_refactor_annotations && decl_file_id.map_or(false, |id| id != t.main_file) { t.cur_file.set(decl_file_id); @@ -991,7 +991,9 @@ pub fn translate( NoItem => {} }; - if t.tcfg.reorganize_definitions && decl_file_id.map_or(false, |id| id != t.main_file) { + if t.tcfg.emit_refactor_annotations + && decl_file_id.map_or(false, |id| id != t.main_file) + { log::debug!("emitting any imports needed by {:?}", decl.kind.get_name()); t.generate_submodule_imports(*top_id, decl_file_id); } @@ -1025,7 +1027,7 @@ pub fn translate( // Header Reorganization: Submodule Item Stores for (file_id, ref mut mod_item_store) in t.items.borrow_mut().iter_mut() { if *file_id != t.main_file { - if tcfg.reorganize_definitions { + if tcfg.emit_refactor_annotations { t.use_feature("register_tool"); } let mut submodule = make_submodule( @@ -1034,7 +1036,7 @@ pub fn translate( *file_id, &mut new_uses, &t.mod_names, - tcfg.reorganize_definitions, + tcfg.emit_refactor_annotations, tcfg.edition, ); let comments = t.comment_context.get_remaining_comments(*file_id); @@ -2110,7 +2112,7 @@ impl<'c> Translation<'c> { .set_mutbl(mutbl); // When putting extern statics into submodules, they need to be public to be accessible - if self.tcfg.reorganize_definitions { + if self.tcfg.emit_refactor_annotations { extern_item = extern_item.pub_(); }; @@ -2597,7 +2599,7 @@ impl<'c> Translation<'c> { let mut mk_ = mk_linkage(true, new_name, name, self.tcfg.edition).span(span); // When putting extern fns into submodules, they need to be public to be accessible - if self.tcfg.reorganize_definitions { + if self.tcfg.emit_refactor_annotations { mk_ = mk_.pub_(); }; @@ -3584,7 +3586,7 @@ impl<'c> Translation<'c> { .ok_or_else(|| format_err!("name not declared: '{}'", varname))?; // Import the referenced global decl into our submodule - if self.tcfg.reorganize_definitions { + if self.tcfg.emit_refactor_annotations { self.add_import(decl_id, &rustname); // match decl { // CDeclKind::Variable { is_defn: false, .. } => {} @@ -5009,7 +5011,7 @@ impl<'c> Translation<'c> { fn insert_item(&self, mut item: Box, decl: &CDecl) { let decl_file_id = self.ast_context.file_id(decl); - if self.tcfg.reorganize_definitions { + if self.tcfg.emit_refactor_annotations { self.use_feature("register_tool"); let attrs = item_attrs(&mut item).expect("no attrs field on unexpected item variant"); add_src_loc_attr(attrs, &decl.loc.as_ref().map(|x| x.begin())); @@ -5027,7 +5029,7 @@ impl<'c> Translation<'c> { fn insert_foreign_item(&self, mut item: ForeignItem, decl: &CDecl) { let decl_file_id = self.ast_context.file_id(decl); - if self.tcfg.reorganize_definitions { + if self.tcfg.emit_refactor_annotations { self.use_feature("register_tool"); let attrs = foreign_item_attrs(&mut item) .expect("no attrs field on unexpected foreign item variant"); diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 1274e12a5d..ace3b1a6ce 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -45,17 +45,17 @@ fn config(edition: RustEdition) -> TranspilerConfig { translate_valist: true, overwrite_existing: true, reduce_type_annotations: false, - reorganize_definitions: false, enabled_warnings: Default::default(), emit_no_std: false, output_dir: None, translate_const_macros: Default::default(), translate_fn_macros: Default::default(), disable_rustfmt: false, - disable_refactoring: false, preserve_unused_functions: false, log_level: log::LevelFilter::Warn, edition, + refactor: false, + emit_refactor_annotations: false, postprocess: false, emit_build_files: false, binaries: Vec::new(), diff --git a/c2rust/src/bin/c2rust-transpile.rs b/c2rust/src/bin/c2rust-transpile.rs index 1fecb1974a..b36d119157 100644 --- a/c2rust/src/bin/c2rust-transpile.rs +++ b/c2rust/src/bin/c2rust-transpile.rs @@ -141,9 +141,14 @@ struct Args { #[clap(long)] reduce_type_annotations: bool, - /// Output file in such a way that the refactoring tool can deduplicate code - #[clap(short = 'r', long)] - reorganize_definitions: bool, + /// Run `c2rust-refactor` after transpiling (and before `--postprocess`). + #[clap(long)] + refactor: bool, + + /// Emit annotations needed by the refactorer for certain transforms, such as `reorganize_definitions`. + /// Implied by `--refactor`. + #[clap(long)] + emit_refactor_annotations: bool, /// Run `c2rust-postprocess` after transpiling and potentially refactoring. /// @@ -291,7 +296,6 @@ fn main() { translate_const_macros: args.translate_const_macros.into(), translate_fn_macros: args.translate_fn_macros.into(), disable_rustfmt: args.disable_rustfmt, - disable_refactoring: args.disable_refactoring, preserve_unused_functions: args.preserve_unused_functions, use_c_loop_info: !args.ignore_c_loop_info, @@ -299,7 +303,8 @@ fn main() { simplify_structures: !args.no_simplify_structures, overwrite_existing: args.overwrite_existing, reduce_type_annotations: args.reduce_type_annotations, - reorganize_definitions: args.reorganize_definitions, + refactor: args.refactor, + emit_refactor_annotations: args.emit_refactor_annotations || args.refactor, postprocess: args.remote_llm_postprocess, emit_modules: args.emit_modules, emit_build_files: args.emit_build_files, diff --git a/scripts/common.py b/scripts/common.py index 65caf71a00..42ee872791 100644 --- a/scripts/common.py +++ b/scripts/common.py @@ -475,7 +475,7 @@ def transpile(cc_db_path: str, use_fakechecks: bool = False, cross_check_config: List[str] = [], incremental_relooper: bool = True, - reorganize_definitions: bool = False) -> bool: + refactor: bool = False) -> bool: """ run the transpiler on all C files in a compile commands database. """ @@ -502,8 +502,8 @@ def transpile(cc_db_path: str, args.append(ccc) if not incremental_relooper: args.append('--no-incremental-relooper') - if reorganize_definitions: - args.append('--reorganize-definitions') + if refactor: + args.append('--refactor') if filter: args.append('--filter') args.append(filter) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index dbb0a1ea62..7ee7a5c75e 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -74,7 +74,7 @@ def __init__(self, log_level: str, path: str, flags: Set[str] = set()) -> None: self.disable_incremental_relooper = "disable_incremental_relooper" in flags self.disallow_current_block = "disallow_current_block" in flags self.translate_const_macros = "translate_const_macros" in flags - self.reorganize_definitions = "reorganize_definitions" in flags + self.refactor = "refactor" in flags self.emit_build_files = "emit_build_files" in flags def translate(self, cc_db: str, edition: RustEdition, ld_lib_path: str, extra_args: List[str] = []) -> RustFile: @@ -102,8 +102,8 @@ def translate(self, cc_db: str, edition: RustEdition, ld_lib_path: str, extra_ar if self.translate_const_macros: args.append("--translate-const-macros") args.append("experimental") - if self.reorganize_definitions: - args.append("--reorganize-definitions") + if self.refactor: + args.append("--refactor") if self.emit_build_files: args.append("--emit-build-files") diff --git a/tests/integration/tests/curl/conf.yml b/tests/integration/tests/curl/conf.yml index e6fdaf08a2..07ac94491f 100644 --- a/tests/integration/tests/curl/conf.yml +++ b/tests/integration/tests/curl/conf.yml @@ -12,7 +12,7 @@ requirements: transpile: autogen: true - # tflags: --reorganize-definitions + # tflags: --emit-refactor-annotations binary: tool_main cargo.transpile: diff --git a/tests/integration/tests/json-c/conf.yml b/tests/integration/tests/json-c/conf.yml index e34b0a02c8..f32099b72e 100644 --- a/tests/integration/tests/json-c/conf.yml +++ b/tests/integration/tests/json-c/conf.yml @@ -6,7 +6,7 @@ requirements: transpile: autogen: true - tflags: --reorganize-definitions --disable-refactoring + tflags: --emit-refactor-annotations cargo.transpile: autogen: true diff --git a/tests/integration/tests/libxml2/conf.yml b/tests/integration/tests/libxml2/conf.yml index a08d945f5f..2d82502837 100644 --- a/tests/integration/tests/libxml2/conf.yml +++ b/tests/integration/tests/libxml2/conf.yml @@ -1,7 +1,7 @@ transpile: autogen: true binary: runtest - tflags: --reorganize-definitions --disable-refactoring + tflags: --emit-refactor-annotations cargo.transpile: autogen: true diff --git a/tests/integration/tests/lua/conf.yml b/tests/integration/tests/lua/conf.yml index 5da76c055c..e093e31eb8 100644 --- a/tests/integration/tests/lua/conf.yml +++ b/tests/integration/tests/lua/conf.yml @@ -7,7 +7,7 @@ requirements: transpile: autogen: true - tflags: --reorganize-definitions --disable-refactoring + tflags: --emit-refactor-annotations binary: lua cargo.transpile: diff --git a/tests/integration/tests/nginx/conf.yml b/tests/integration/tests/nginx/conf.yml index 7e79d493ad..61c2f6b93a 100644 --- a/tests/integration/tests/nginx/conf.yml +++ b/tests/integration/tests/nginx/conf.yml @@ -9,7 +9,7 @@ requirements: transpile: autogen: true # blocked on https://github.com/immunant/c2rust/issues/266 - tflags: --reorganize-definitions --disable-refactoring + tflags: --emit-refactor-annotations binary: nginx cargo.transpile: diff --git a/tests/integration/tests/python2/conf.yml b/tests/integration/tests/python2/conf.yml index e613ea9951..2da572e550 100644 --- a/tests/integration/tests/python2/conf.yml +++ b/tests/integration/tests/python2/conf.yml @@ -17,7 +17,7 @@ requirements: transpile: autogen: true - tflags: --reorganize-definitions --disable-refactoring + tflags: --emit-refactor-annotations binary: python cargo.transpile: diff --git a/tests/integration/tests/ruby/conf.yml b/tests/integration/tests/ruby/conf.yml index 72fb6bc480..c9ee517cce 100644 --- a/tests/integration/tests/ruby/conf.yml +++ b/tests/integration/tests/ruby/conf.yml @@ -9,7 +9,7 @@ requirements: transpile: autogen: true - tflags: --reorganize-definitions --disable-refactoring + tflags: --emit-refactor-annotations binary: ruby # cflags: "-O0" diff --git a/tests/integration/tests/snudown/conf.yml b/tests/integration/tests/snudown/conf.yml index b78f176054..1e2c20478a 100644 --- a/tests/integration/tests/snudown/conf.yml +++ b/tests/integration/tests/snudown/conf.yml @@ -7,7 +7,7 @@ requirements: transpile: autogen: true - tflags: --reorganize-definitions --disable-refactoring + tflags: --emit-refactor-annotations cargo.transpile: autogen: true diff --git a/tests/integration/tests/zstd/conf.yml b/tests/integration/tests/zstd/conf.yml index 2bfb4e64e8..8df3a21bb0 100644 --- a/tests/integration/tests/zstd/conf.yml +++ b/tests/integration/tests/zstd/conf.yml @@ -8,7 +8,7 @@ transpile: autogen: true binary: fullbench # zstdcli cflags: -Qunused-arguments - tflags: --reorganize-definitions --disable-refactoring + tflags: --emit-refactor-annotations cargo.transpile: autogen: true diff --git a/tests/unit/comments/src/comments.c b/tests/unit/comments/src/comments.c index f85b0f5417..f04da93013 100644 --- a/tests/unit/comments/src/comments.c +++ b/tests/unit/comments/src/comments.c @@ -1,4 +1,4 @@ -//! translate_const_macros, reorganize_definitions, emit_build_files +//! translate_const_macros, refactor, emit_build_files /* top level doc comment * second line */ diff --git a/tests/unit/items/src/test_fn_attrs.rs b/tests/unit/items/src/test_fn_attrs.rs index 4a77fee957..ad51b4a42b 100644 --- a/tests/unit/items/src/test_fn_attrs.rs +++ b/tests/unit/items/src/test_fn_attrs.rs @@ -6,8 +6,8 @@ pub fn test_fn_attrs() { // so instead we're checking the source itself let src = include_str!("fn_attrs.rs"); - // Remove the c2rust::src_loc annotation, which is only produced if - // --reorganize-definitions is enabled. + // Remove the `c2rust::src_loc` annotation, which is only produced if + // `--emit-refactor-annotations` is enabled. let mut lines = src.lines().collect::>(); lines.retain(|x| !x.contains("#[c2rust::src_loc")); let src = lines.join("\n"); diff --git a/tests/unit/modules/src/modules.c b/tests/unit/modules/src/modules.c index acc3290a6b..728862ed53 100644 --- a/tests/unit/modules/src/modules.c +++ b/tests/unit/modules/src/modules.c @@ -1,4 +1,4 @@ -//! translate_const_macros, reorganize_definitions +//! translate_const_macros, refactor #include #include "other_mod2.h" diff --git a/tests/unit/statics/src/test_sections.rs b/tests/unit/statics/src/test_sections.rs index 964183f83e..19fdb96455 100644 --- a/tests/unit/statics/src/test_sections.rs +++ b/tests/unit/statics/src/test_sections.rs @@ -40,8 +40,8 @@ pub fn test_sectioned_used_static() { let mut lines = src.lines().collect::>(); - // Remove the c2rust::src_loc annotation, which is only produced if - // --reorganize-definitions is enabled. + // Remove the `c2rust::src_loc` annotation, which is only produced if + // `--emit-refactor-annotations` is enabled. lines.retain(|x| !x.contains("#[c2rust::src_loc")); let src = lines.join("\n"); @@ -65,7 +65,7 @@ pub static mut rust_initialized_extern: ::core::ffi::c_int = 1 as ::core::ffi::c "# .trim(), )); - // This static is pub only with --reorganize-definitions + // This static is pub only with `--emit-refactor-annotations`. let aliased_static_syntax = |public| { format!( r#"