Missing AST Node for parsing code with std::vector<unsigned int>::data()

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
struct MyTestVector2
{
T _data[6];
T * data() { return &_data[0]; }
};


MyTestVector2 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’:‘MyTestVector2’

This time, The AST node for kernel_TestColor is just missed!

The code is absolutely correct itself, it can be build and run.

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!

You might need to provide more details - at least a cursory example shows the function call in the ast dump: https://godbolt.org/z/zvqTa3

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
struct MyTestVector2
{
T _data[6];
T * data() { return &_data[0]; }
};


MyTestVector2 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’:‘MyTestVector2’

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.

Richard, thank you very much for the answer!

>>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
Yes, exactly!

In fact David already helps me to find the problem )
Sorry i didnt wrote it here, used such mailing list first time!

I have create a minimal example which helps me to catch it

It seems that i have specified language options too late … :

data/input.cpp:1:10: error: unknown type name ‘class’
template

https://github.com/FROL256/clang_parse_ast_example/blob/main/main.cpp

(lines 162–170, wrong way, too late)

(lines 148–155, right way, works)

I think that in my main project i didn’t got such error message (unknown type name ‘class’) due to other errors related to missed includes.
After I add some stl types definition and some functions from to processed source code, … then finally i got AST parsed correcty.

Thank you very much, clang is awesome!