[cfe-dev] walking macros with clang-c

Douglas Gregor dgregor at apple.com
Wed May 11 21:14:30 PDT 2011


On May 11, 2011, at 6:23 PM, Daniel Herring wrote:

> Hi,
> 
> I am trying to use clang-c/Index.h to analyze header files.  I can access 
> most things of interest, but macro definitions appear to be completely 
> opaque.
> 
> Is there a clang-c equivalent of the MacroInfo class?
> http://clang.llvm.org/doxygen/classclang_1_1MacroInfo.html
> 
> In particular, I want to know isFunctionLike/isObjectLike and to access 
> the argument and token lists.  Variadic and builtin information would be 
> nice but not necessary at the moment.
> 
> Here are two example macros that I'd like to parse.  They are from 
> H5Tpublic.h in the HDF5 library.
> 
> #define H5OPEN          H5open(),
> ...
> #define H5T_IEEE_F32BE              (H5OPEN H5T_IEEE_F32BE_g)

libclang doesn't actually expose that information, but it would be really cool if it did.

> P.S.  If the answer is "help wanted, shouldn't be hard to add", please 
> direct me to the relevant source files.  Otherwise, I can stick with my 
> existing hodge-podge of sed scripts for macros.

Yeah, that's pretty much the answer for this one :)

Here's how I'd tackle it: add a function that takes a macro-definition cursor, which would go into tools/libclang/CIndex.cpp. This function would re-preprocess that line of source code to create a MacroInfo object and return information about it (e.g., the tokens can be CXTokens, the macro arguments can be an array of strings, etc.). 

There are other implementation approaches, but they would require more invasive changes.

	-Doug




More information about the cfe-dev mailing list