[cfe-dev] [libClang] Parsing expanded function-like macros

Fāng-ruì Sòng via cfe-dev cfe-dev at lists.llvm.org
Thu Mar 1 14:36:04 PST 2018


On 2018-03-01, Joe Turner wrote:
>Thanks to the pointer, but as far as I can tell, that is not actually ever
>called when running the Clang C API (nor is handleDeclOccurence from what I can
>tell).

It is not called. `handleDeclOccurence` will be called when you use clang_indexTranslationUnit.
But there is no way in libclang to get the source location in macro replacement-list.

>
>When doing clang -Xclang -ast-dump, I get basically what I want (the macro is
>fully expanded), so there must be some way of coercing libclang to give me that
>same information.

I hope some one could implement this for libclang :)
rtags emulates one-level macro expansion in a not-so-elegant way https://github.com/Andersbakken/rtags/blob/master/src/ClangIndexer.cpp#L1055
If you find yourself at a loss, rtags-find-symbol-at-point can be used instead as a poor man's replacement.

>Cheers,
>Joe
>
>On Wed, Feb 28, 2018 at 11:45 PM, Fāng-ruì Sòng <maskray at google.com> wrote:
>
>    Is this relevant?
>    clang::index::IndexDataConsumer::HandleMacroOccurence is not
>    implemented (overriden) in clang/tools/libclang/CXIndexDataConsumer.h
>
>    I guess CXTranslationUnit_DetailedPreprocessingRecord only makes macro
>    expansions children of the translation unit AST, there is still no way
>    to get their position in the macro replacement-list. I would also like
>    to see the feature implemented in libclang.
>
>    On Wed, Feb 28, 2018 at 5:35 PM, Joe Turner via cfe-dev
>    <cfe-dev at lists.llvm.org> wrote:
>    > I'm using libClang to parse the AST for Objective-C/C++ code, and have
>    run
>    > into an issue with macro-like functions. Given something like this:
>    >
>    > #define FOO(a, b) a + b
>    >
>    > int main() {
>    >   int a = 0;
>    >   int b = 1;
>    >   int c = FOO(a, b);
>    >   return c;
>    > }
>    >
>    > I would like to be able to see references to a and b in the AST when I
>    parse
>    > the "int c = FOO(a, b);" line. However, all I see in the AST is the "int
>    c =
>    > " part (a Decl and a VarDecl).
>    >
>    > I've done a bunch of searching, and people mention using
>    > CXTranslationUnit_DetailedPreprocessingRecord, which doesn't work for me.
>    > This adds children to the translation unit that reference the macro
>    itself,
>    > but it doesn't give me access to the arguments passed into the
>    function-like
>    > macro.
>    >
>    > Short of using the C++ API, is there any way to get this information from
>    > libClang?
>    >
>    > Thanks!
>    > Joe



More information about the cfe-dev mailing list