Skip to content

Commit 3b61146

Browse files
committed
Add --preserve-unused-functions
to keep otherwise pruned static / inline functions. Closes: immunant#287
1 parent 514b53b commit 3b61146

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl TypedAstContext {
519519
}
520520
}
521521

522-
pub fn prune_unused_decls(&mut self) {
522+
pub fn prune_unused_decls(&mut self, preserve_unused_functions: bool) {
523523
// Starting from a set of root declarations, walk each one to find declarations it
524524
// depends on. Then walk each of those, recursively.
525525

@@ -545,6 +545,13 @@ impl TypedAstContext {
545545
to_walk.push(decl_id);
546546
used.insert(decl_id);
547547
}
548+
CDeclKind::Function {
549+
body: Some(_),
550+
..
551+
} if preserve_unused_functions => {
552+
to_walk.push(decl_id);
553+
used.insert(decl_id);
554+
}
548555
CDeclKind::Variable {
549556
is_defn: true,
550557
is_externally_visible: true,

c2rust-transpile/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ pub struct TranspilerConfig {
109109
pub translate_const_macros: bool,
110110
pub translate_fn_macros: bool,
111111
pub disable_refactoring: bool,
112+
pub preserve_unused_functions: bool,
112113
pub log_level: log::LevelFilter,
113114

114115
// Options that control build files

c2rust-transpile/src/translator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ pub fn translate(
530530

531531
// Headers often pull in declarations that are unused;
532532
// we simplify the translator output by omitting those.
533-
t.ast_context.prune_unused_decls();
533+
t.ast_context.prune_unused_decls(tcfg.preserve_unused_functions);
534534

535535
enum Name<'a> {
536536
VarName(&'a str),

c2rust/src/bin/c2rust-transpile.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ fn main() {
8989
translate_const_macros: matches.is_present("translate-const-macros"),
9090
translate_fn_macros: matches.is_present("translate-fn-macros"),
9191
disable_refactoring: matches.is_present("disable-refactoring"),
92+
preserve_unused_functions: matches.is_present("preserve-unused-functions"),
9293

9394
use_c_loop_info: !matches.is_present("ignore-c-loop-info"),
9495
use_c_multiple_info: !matches.is_present("ignore-c-multiple-info"),

c2rust/src/transpile.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ args:
172172
long: disable-refactoring
173173
help: Disable running refactoring tool after translation
174174
takes_value: false
175+
- preserve-unused-functions:
176+
long: preserve-unused-functions
177+
help: Include static and inline functions in translation
178+
takes_value: false
175179
- log-level:
176180
long: log-level
177181
help: Logging level

0 commit comments

Comments
 (0)