c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

Add option to translate all internal functions (was: Transpiling libdivide results in an empty file)

Open thedrow opened this issue 5 years ago • 9 comments

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?

thedrow avatar Jun 11 '19 09:06 thedrow

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.

rinon avatar Jun 11 '19 22:06 rinon

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?

thedrow avatar Jun 12 '19 08:06 thedrow

Yeah, I was thinking the same thing while writing up a response. I'll look into that, shouldn't be too hard.

rinon avatar Jun 12 '19 20:06 rinon

Any news on this one?

thedrow avatar Aug 06 '19 05:08 thedrow

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?

rinon avatar Aug 13 '19 23:08 rinon

Yes it did :)

thedrow avatar Aug 14 '19 06:08 thedrow

Pinging again :)

thedrow avatar Jan 27 '20 10:01 thedrow

Thanks for the ping, fell off my list. I'll make sure something happens on this.

rinon avatar Jan 30 '20 18:01 rinon

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.

chrysn avatar Jan 17 '22 13:01 chrysn