[cfe-dev] libclang or libtooling for transpiler

Stephen Crane via cfe-dev cfe-dev at lists.llvm.org
Mon Apr 29 12:07:20 PDT 2019


We had the exact same question when we started work on C2Rust. We
ended up using the libtooling API with C++ to extract the AST and
serialize it into Rust. We found that the libclang interface was
insufficient to extract all information we needed for the transpiler.
If I recall correctly, bindgen is hitting up against similar issues
since they use libclang. Rather than try to extend the C API, we just
used the libtooling interface to get direct access to all the internal
Clang data structures. We've supported LLVM 6-8 under this approach
without significant compatibility issues or churn.

We originally chose to separate the AST exporter (c2rust-ast-exporter)
from the rest of the transpiler as a standalone tool, so it serializes
the AST into a CBOR format. I would recommend not using this approach,
and we're planning to remove it. In the future, the AST exporter will
interface more directly with Rust and build a Rust representation of
the AST and all necessary metadata, rather than serializing it back
and forth to a format like CBOR.

I'd be happy to discuss C(++) to Rust transpiling further if you'd
like, although much of that discussion is probably not relevant to
cfe-dev.

- stephen

On Mon, Apr 29, 2019 at 3:12 AM Jacob Carlborg via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
>
> On 2019-04-29 12:06, Jacob Carlborg via cfe-dev wrote:
>
> > I've created a tool that does basically the same thing. It generate D
> > bindings to C and Objective-C libraries. This tool is written in D and
> > is using libclang [1]. It's been working for me so far, but I haven't
> > got to the point of trying to translate C++ headers yet.
> >
> > This tool is only focusing on generating bindings. That means only the
> > header files are translated, not full C/Objective-C code with function
> > bodies with statements and expressions.
> >
> > The current libclang bindings DStep is using are generated with DStep
> > itself. The initial bindings were created by had. It was very easy to
> > do, just some search and replace. libclang contains the most forgiving
> > header files, from this point of view, I have ever seen.
> >
> > [1] http://github.com/jacob-carlborg/dstep
>
> Forgot the summary:
>
> I chose libclang because:
>
> * It's a stable API
> * It's what's recommend if using other language than C++
> * I really, really did not want to use C++. After all, I'm using D, not
> C++ :)
>
> I would say it depends on how much you don't want to use C++. If I were
> you, I would definitely give it a try in Rust.
>
> --
> /Jacob Carlborg
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


More information about the cfe-dev mailing list