c2rust
c2rust copied to clipboard
Add option to translate all internal functions (was: Transpiling libdivide results in an empty file)
I've run c2rust 0.10.1 on libdivide.
I got the following:
#![allow(dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut)]
extern crate libc;
My compile_commands.json
is:
[
{
"directory": "/home/omer/Documents/Projects/libdivide",
"command": "/home/linuxbrew/.linuxbrew/bin/cc -I/home/omer/Documents/Projects/libdivide -march=native -fno-tree-vectorize -O0 -DNDEBUG -c /home/omer/Documents/Projects/libdivide/libdivide.h",
"file": "/home/omer/Documents/Projects/libdivide/libdivide.h"
}
]
I expected all the methods to be transpiled. What could be the problem?
C2Rust is currently designed to translate only functions that are actually used in a C project. This means that it translates main
for binaries, any functions that have public visibility (i.e. not static
), and all code these functions depend on. The reason for this is to avoid translating everything inside system header (e.g., stdio.h
) even though the program uses only a tiny part of that header.
Since libdivide is a header only library, it does not have a publicly visible API outside of that header. Effectively C2Rust is seeing that all functions have the static
attribute but are never used so skips them. You should remove the static
attribute from the LIBDIVIDE_API
macro for the translation so the public API is visible and C2Rust sees that it needs to translate these functions.
While that works it's not the workflow I'd expect.
Maybe there should be a flag to ignore the static flag and emit the code anyway?
Yeah, I was thinking the same thing while writing up a response. I'll look into that, shouldn't be too hard.
Any news on this one?
Haven't had a chance to look into a flag for this yet, but it's still on my radar. I take it modifying the LIBDIVIDE_API
macro worked for you, though?
Yes it did :)
Pinging again :)
Thanks for the ping, fell off my list. I'll make sure something happens on this.
There is a flag that makes C2Rust keep the statics, it is --preserve-unused-functions
.
It pulls in a load of unused stuff, but might help you.