[cfe-dev] Using Clang libraries to query declarations

Steve Cheng steve.ckp at gmail.com
Sat May 16 08:00:38 PDT 2015

On 2015-05-07 20:36:02 -0400, Steve Cheng said:

> I'm thinking of building a tool to generate bindings to a C++ library, 
> obviously using Clang to parse C++ header files, and would appreciate 
> your help.
> Actually, to keep things concrete and simpler, let's say we want to 
> write a tool that reads some header files, and lets the user type in 
> qualified names of C++ types and functions, and then the tool reports 
> what it actually is. For example, we might #include <string> and submit 
> a query for "std::string::length", and then the tool could tell us that


Replying to myself, but I did have a positive resolution and wanted to 
share my findings, should anybody be interested.

By using the C++ libraries, not libclang, I found I could control 
clang::Parser precisely, so it first parses the header files I need, 
then individual C++ qualified-ids and type-ids in isolation. And I can 
feed what comes out to clang::Sema.

I then realized that I only needed semantic information and not so much 
the lexical information from the C++ headers. So there's not much I 
could reuse from libclang anyway, which is primarily designed around 
AST cursors. I originally wanted to use libclang because it seemed to 
be recommended for API stability, and my frontend isn't able to link to 
LLVM's C++ libraries directly. But I can build C interfaces myself for 
the stuff I'm extracting with Clang.


More information about the cfe-dev mailing list