Using Clang to import C++ source in an interpreter

I am working on an interpreter for a computer language of my own design, and I’d like to allow my language to directly import C++ files.

I need a clang libtool for a project I am working on. I need a function that can take in a file name of a C++ source or header file and return a structure that represents the structure of the source file. Many IDE’s have a source tree that shows the global variables, functions, classes, etc… that are defined in a C++ file. I need a functional version of that. What would the best way of producing such a function be? Would I make a clang libtool? How would I structure the tool? Is there any code or tool that I could look at for guidance?

I have seen a number of the simple clang-tool examples online, but I have no idea how to extend them to do what I want.

Furthermore, once I get a tree of variables, functions, and such, is there some way to JIT compile the functions and call them passing data from my external program to the JITed functions and back again?

Brian Hare

Hi there,

this is definitely possible, but you’ll find that it’s not entirely straightforward. As a starting point however,
I can point you to the two projects which do this already. Maybe you’ll find the code useful.

One is the Julia C++ Interface at which uses Clang to import C++ into julia.
The other is CERN’s Cling at

There’s some quirks to both of these. Cxx.jl for example doesn’t include any of the JIT-related stuff, because it expects to just be able to hand off LLVM Function* pointers to the julia execution engine. Cling on the other hand includes a lot of stuff specific to CERN’s usage of it in their ROOT framework.

I’d recommend you take a look at the two links and see if they are helpful.

Okay, I’ll defintly look at Julia and Cling as a starting point.