[cfe-dev] Extracting macro information using libclang, the C Interface to Clang

Kevin Funk kfunk at kde.org
Thu Nov 27 23:08:53 PST 2014

On Thursday 27 November 2014 18:47:17 Mark wrote:
> Hello all,
> I'm new to libclang and using the Clang C-interface (version 3.6.0,
> 222169) to parse some C header files.
> My goal is to extract all the #define constants from the .h files.
> For example, for the following #defines:
> #define FEE 12
> #define FIE 34
> #define FOE (FEE+FIE)
> #define FUM FIE
> I would like to get both the names and the resulting raw values as follows:
> FEE = 12
> FIE = 34
> FOE = 46
> FUM = 34
> I've got as far as handling /CXCursor_MacroDefinition, /and getting the
> macro name from the cursor spelling. But after that, it's not clear to
> me how I might obtain further information about the defined macro values
> themselves.

Yep. That's where we are as well. Getting the definitions + their uses is 

Context: I'm the one working on (lib)Clang integration in KDevelop.

> Is this possible using the libclang C interface?
> If so, could someone please provide an example, or suggest an approach
> that that might work?

I was looking into this several months ago [1], too, and didn't find a 
solution so far. I'd love to get some API in libclang for achieving this.

The open-source woboq code browser (code.woboq.org) has this feature, but uses 
C++-API of Clang; so it's definitely possible, but I still didn't find the 
time to map this implementation to libclang.

Example usage:
  hover over 'Q_OBJECT'.

The code dealing with Clang's C++ API is here:

Note: If you're interested in getting this into libclang I'd be glad to help 

[1] http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-August/038543.html

> Thank you,
> Mark.

Kevin Funk | kfunk at kde.org | http://kfunk.org

More information about the cfe-dev mailing list