using python clang.cindex library to generate bindings ?

hi there,

I just noticed that llvm/clang-2.7 provides a ctypes-based library
wrapping libCIndex.so for python.
Is it possible, planned or adviseable to use such a thing to
programatically generate a representation (say in json/xml) of some
C/C++ api so it could be then used to automatically generate bindings
in other high-level languages ?

cheers,
sebastien.

hi there,

I just noticed that llvm/clang-2.7 provides a ctypes-based library
wrapping libCIndex.so for python.
Is it possible, planned or adviseable to use such a thing to
programatically generate a representation (say in json/xml) of some
C/C++ api so it could be then used to automatically generate bindings
in other high-level languages ?

It seems possible, but now one is working on it as far as I know. As
far as advisable, its certainly a better approach than doing ad hoc
parsing of C/C++ code.

- Daniel

I have been looking for such a thing myself. Working on Synopsis (http://synopsis.fresco.org), I'm interested into CLang to generate / populate Synopsis' abstract semantic graph (ASG) for the parsed code, which I then could manipulate further using Synopsis scripts (for example to generate API documentation).

It seems the CIndex API is by far not rich enough for my needs, and I'm not sure whether that will change. I'm now looking into CLang's own AST et al. APIs instead, even though that's said to be much less stable than CIndex.

FWIW,
         Stefan

hi Daniel,

Excerpts from Daniel Dunbar's message of 2010-05-03 18:26:27 +0200:

> hi there,
>
> I just noticed that llvm/clang-2.7 provides a ctypes-based library
> wrapping libCIndex.so for python.
> Is it possible, planned or adviseable to use such a thing to
> programatically generate a representation (say in json/xml) of some
> C/C++ api so it could be then used to automatically generate bindings
> in other high-level languages ?

It seems possible, but now one is working on it as far as I know. As
far as advisable, its certainly a better approach than doing ad hoc
parsing of C/C++ code.

ok, so if I were to volunteer myself (always a bit dangerous,) where
should I start ?
I suppose some struct to store the various DeclXyz informations would
have to be devised, or some way to expose the various bits and pieces
of what makes wrapping an API "interesting" (function arguments, their
type, name and default values, classes/structs - their fields, names,
types, access-specifiers,...)

(I actually started a JsonPrinter visitor modeled after the
PrintFunctionNames plugin example, as hacking on a little plugin .so
is easier on the turn-around and easier for out-of-tree development)

cheers,
sebastien.

We make absolutely no stability guarantees for interacting with the AST through C++. Feel free to write code against it, but understand that your code will be version-locked and may need to be updated in possibly subtle ways every time you bump clang versions. That said, it's absolutely going to be a richer interface than libclang (f.k.a. CIndex).

John.

Until it exists, you might want to check out SWIG, which parses a subset of C++ and automatically generates bindings in other high-level languages.

Best,
Martin