The attached patch (git format, based on SVN trunk revision 131770) adds a few functions to libclang for inspecting macros. Haven't built against current trunk (132387) but the patch applies cleanly. It is not fully tested, but I wanted to get it out for others to comment on, test, fix it up for commit, etc.
notes:
- I tried to mimic the code style, but haven't really read the standards...
- original plan was to re-lex the macro. This didn't work because the currently exposed source locations point to the macro name, not the #define... I couldn't see any obvious way to find it (especially with end-of-line backslashes).
- The preprocessing record maps MacroInfo -> MacroDefinition, but not the other way around... I hacked in something but don't know all the implications.
- clang_getMacroTokens could use some review, in particular the missing source location and whether previousWasAt makes sense (I don't know ObjC)
- clang_isMacroFunctionLike and clang_getMacroTokens seem to work
- clang_isMacroBuiltin isn't reporting true anywhere, even for many macros that should be (e.g. __clang_version__)
- haven't tried to test clang_isMacroVariadic
- didn't touch the exports for Darwin since I don't have OS X handy at home
For reference, here's a snippet from my test harness. Mostly C, but compiled under g++...
if(kind==CXCursor_MacroDefinition)
{
if(clang_isMacroBuiltin(cursor))
printf("\t(builtin)\n");
if(clang_isMacroFunctionLike(cursor))
printf("\t(functionlike)\n");
if(clang_isMacroVariadic(cursor))
printf("\t(variadic)\n");
CXToken *args, *tokens;
unsigned numArgs, numTokens;
clang_getMacroTokens(cursor, &args, &numArgs, &tokens, &numTokens);
for(int i=0; i<numArgs; i++)
{
CXString s=clang_getTokenSpelling(tu, args[i]);
printf("%s arg: %s\n", indent, clang_getCString(s));
clang_disposeString(s);
}
for(int i=0; i<numTokens; i++)
{
CXString s=clang_getTokenSpelling(tu, tokens[i]);
printf("%s token: %s\n", indent, clang_getCString(s));
clang_disposeString(s);
}
clang_disposeTokens(tu, args, numArgs);
clang_disposeTokens(tu, tokens, numTokens);
}
Later,
Daniel