[cfe-users] Missing AST Node for parsing code with std::vector<unsigned int>::data()

Richard Smith via cfe-users cfe-users at lists.llvm.org
Mon Dec 14 23:15:25 PST 2020


On Thu, 3 Dec 2020 at 04:02, Владимир Фролов via cfe-users <
cfe-users at lists.llvm.org> wrote:

> Greetings! I'm using clang for source-to-source translation.
>
> Recently I got a problem with parsing code which use templates.
> First, here is the working example:
>
> struct MyTestVector2
> {
>  unsigned int _data[6];
>  unsigned int * data() { return &_data[0]; }
> };
> ...
> MyTestVector2 testData2;
> kernel_TestColor(&hit,
>                 testData2.data(), tidX, tidY);
> This code processed normally and i gon correct "CXXMemberCallExpr" node
> for kernel_TestColor which i actually need.
> Then i changed MyTestVector2 to template:
>
> template<class T>
> struct MyTestVector2
> {
>  T _data[6];
>  T * data() { return &_data[0]; }
> };
>
> ...
> MyTestVector2<unsigned int> testData2;
> kernel_TestColor(&hit,
>                 testData2.data(), tidX, tidY);
>
> This time i got
>
> `-DeclStmt 0x55555816e128 <line:59:3, col:40>
>   `-VarDecl 0x55555816e0c0 <col:3, col:31> col:31 invalid testData2
> 'MyTestVector2<unsigned int>':'MyTestVector2<unsigned int>'
>
> This time, The AST node for kernel_TestColor is just missed!
> The code is absolutely correct itself, it can be build and run.
>

What interface are you using to parse the code with Clang? (libclang?
libTooling? Direct use of the C++ API?)

The above AST dump shows that Clang thinks the declaration of the variable
'testData2' is invalid (see the "invaild" in the declaration). Clang should
also have produced a diagnostic message explaining this, but some of the
ways of using Clang as a library don't set up a diagnostic consumer by
default, meaning that error message is just being thrown away. If you set
up a diagnostic consumer, Clang should tell you what's going wrong.

I suspect the problem is either something you elided in the above code or
it's an issue with the compilation flags in use, because the above example
in isolation looks fine.


> I'm using AST processing for code generation, so it is important for me to
> get  AST node for kernel_TestColor and then analyze its arguments.
> I understand that this seems to be not a bug, but ... may be, there is a
> way to make parser more tolerant to data types ... because what i actually
> need is just a strings of all parameters, so, just having "
> testData2.data()" at some level of AST for the second parameter of kernel_TestColor
> would be enough for me.
>
> I would be appretiate for help or advice.
> Currently i use llvm 10.
> Thanks!
> --
> Best Regards,
>  Frolov V.A.
>
> _______________________________________________
> cfe-users mailing list
> cfe-users at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20201214/35a73464/attachment.html>


More information about the cfe-users mailing list