[libClang] Parsing expanded function-like macros

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[libClang] Parsing expanded function-like macros

Joerg Sonnenberger via cfe-dev
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

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [libClang] Parsing expanded function-like macros

Joerg Sonnenberger via cfe-dev
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
<[hidden email]> 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
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>



--
宋方睿
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [libClang] Parsing expanded function-like macros

Joerg Sonnenberger via cfe-dev
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).

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.

Cheers,
Joe

On Wed, Feb 28, 2018 at 11:45 PM, Fāng-ruì Sòng <[hidden email]> 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
<[hidden email]> 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
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>



--
宋方睿


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [libClang] Parsing expanded function-like macros

Joerg Sonnenberger via cfe-dev
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 <[hidden email]> 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
>    <[hidden email]> 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
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev