[SSAF][PointerAnalysis] Extract from C++ binding declarations
The following examples involving BindingDecls cannot be handled by existing pointer analysis:
void f(int *p int *q) {
{
int *Arr[2] = {p, q};
auto [a, b] = Arr;
}
void g(int *p int *q) {
{
std::pair<int*, int*> Pair = {p, q};
auto [a, b] = Pair;
}
void h(int *p int *q) {
{
struct {int *x; int *y;} S;
auto [a, b] = S;
}
The AST of the binding declaration is like:
-DeclStmt
| `-DecompositionDecl <col:3, col:17> col:8 used 'int *[2]' cinit
| |-ArrayInitLoopExpr <col:17> 'int *[2]'
| | |-...
| |-BindingDecl <col:9> col:9 a 'int *'
| | `-ArraySubscriptExpr <col:9> 'int *' lvalue
| | |-ImplicitCastExpr <col:9> 'int **' <ArrayToPointerDecay>
| | | `-DeclRefExpr <col:9> 'int *[2]' lvalue Decomposition 0x48167c20 first_binding 'a' 'int *[2]'
| | `-IntegerLiteral <col:9> 'int' 0
| | ....
A DecompositionDecl, which is a special kind of VarDecl, is introduced to hold the value of the initializer in the spelling.
For each binding BindingDecl, it is initialized then by an expression E (in this AST E is an array subscript) over the DecompositionDecl representing the corresponding sub-object.
The direct issue is that a DecompositionDecl is not an entity so that neither itself not E can be represented as EntityPointerLevels.
@llvm/issue-subscribers-clang-ssaf
Author: Ziqing Luo (ziqingluo-90)
void g(int p int q) {
{
std::pair<int, int> Pair = {p, q};
auto [a, b] = Pair;
}
void h(int *p int *q) {
{
struct {int *x; int *y;} S;
auto [a, b] = S;
}
The AST of the binding declaration is like:
-DeclStmt
| -DecompositionDecl <col:3, col:17> col:8 used 'int *[2]' cinit | |-ArrayInitLoopExpr <col:17> 'int *[2]' | | |-... | |-BindingDecl <col:9> col:9 a 'int *' | | -ArraySubscriptExpr <col:9> 'int *' lvalue
| | |-ImplicitCastExpr <col:9> 'int **' <ArrayToPointerDecay>
| | | -DeclRefExpr <col:9> 'int *[2]' lvalue Decomposition 0x48167c20 first_binding 'a' 'int *[2]' | | -IntegerLiteral <col:9> 'int' 0
| | ....
A `DecompositionDecl`, which is a special kind of `VarDecl`, is introduced to hold the value of the initializer in the spelling.
For each binding `BindingDecl`, it is initialized then by an expression `E` over the `DecompositionDecl` representing the corresponding sub-object.
The direct issue is that a `DecompositionDecl` is not an entity so that neither itself not `E` can be represented as EntityPointerLevels.
</details>
I think we should handle it the same way as the desugared form would look like:
#include <utility>
void f(int *p, int *q) {
int *Arr[2] = {p, q};
auto [a, b] = Arr;
}
void g(int *p, int *q) {
std::pair<int*, int*> Pair = {p, q};
auto [a, b] = Pair;
}
void h(int *p, int *q) {
struct {int *x; int *y;} S;
auto [a, b] = S;
}
Is desugared into: https://compiler-explorer.com/z/b794nh36E (click on the CppInsights button there)
#include <utility>
void f(int * p, int * q)
{
int * Arr[2] = {p, q};
int * __Arr5[2] = {Arr[0], Arr[1]}; // `__Arr5` is what the DecomposedDecl represents - this is what behaves as if it was a 'VarDecl'.
int *& a = __Arr5[0];
int *& b = __Arr5[1];
}
void g(int * p, int * q)
{
std::pair<int *, int *> Pair = std::pair<int *, int *>{p, q};
std::pair<int *, int *> __Pair10 = std::pair<int *, int *>(Pair); // <--- __Pair10
int *&& a = std::get<0UL>(static_cast<std::pair<int *, int *> &&>(__Pair10));
int *&& b = std::get<1UL>(static_cast<std::pair<int *, int *> &&>(__Pair10));
}
void h(int * p, int * q)
{
struct __anon_14_3
{
int * x;
int * y;
};
__anon_14_3 S;
__anon_14_3 __S15 = __anon_14_3(S); // <--- __S15
int *& a = __S15.x;
int *& b = __S15.y;
}
I think we should handle it the same way as the desugared form would look like: ...
@steakhal Right. The "desugaring" introduces a new variable with a naming prefix __, which corresponds to the DecompositionDecl in the AST. So the question really is "does DecompositionDecl represent an entity?".. It is a variable declaration without a name, so there is currently no USR for it. But I think it could be. Without it, the copy from the original initializer, for example, from Arr to __Arr5, might be lost in translation.
I have a separate idea arising from this issue that might be worth comments from people: I started to think that the EntityPointerLevel and PointerFlow extraction do not have to be tightly coupled with Entity as it currently is.
- The EntityPointerLevelTranslator could output a pair
(Decl, PtrLv)instead of(Entity, PtrLv). The former can be further converted to the latter. This way, the EntityPointerLevelTranslator could act more like an access-path extractor and would not have to deal with errors related to entities. - In PointerFlow graphs, nodes could be generalized to wrap (a few more) different kinds of data; EntityPointerLevel is one of them. This could give us the flexibility of, for example, introducing intermediate nodes to connect nodes of interest. I imagine this could be useful if we want to remove local entities from graphs--the removal may require introducing intermediate simple nodes.
- A combination of the above two can be an alternative solution to this problem:
__Arr5[0]would be represented in the graph as a node wrapping aDecompositionDecland the pointer level, no entity involved.
CC @jkorous-apple @aviralg @dtarditi @t-rasmud
I think we should handle it the same way as the desugared form would look like: ...
@steakhal Right. The "desugaring" introduces a new variable with a naming prefix
__, which corresponds to theDecompositionDeclin the AST. So the question really is "does DecompositionDecl represent an entity?".. It is a variable declaration without a name, so there is currently no USR for it. But I think it could be. Without it, the copy from the original initializer, for example, fromArrto__Arr5, might be lost in translation.
It's a stretch that it introduces any variable (because it doesn't) - but what I'm saying is that we should treat it as-if it would. This also means that we should be able to create a USR for it - to create an Entity for it.
- The EntityPointerLevelTranslator could output a pair (Decl, PtrLv) instead of (Entity, PtrLv). The former can be further converted to the latter. This way, the EntityPointerLevelTranslator could act more like an access-path extractor and would not have to deal with errors related to entities.
I have no problem with this - but I know that @jkorous-apple might have a strong opinion here.
I can't comment on the rest of your options without catching up on the current implementation.
Please note that CppInsights is just a visualizer tool. It does not reflect how clang internally thinks about the code. So internally, there no such array, or anonymous types or anything.
@flovent Yes, everyone is welcome to contribute to SSAF.
I can't comment on the rest of your options without catching up on the current implementation.
I should have brought up the context here. I will do it later.
We should extend entities so they can represent BindingDecl.
I don't see the need to model implementation details of bindings. Why don't we treat the identifiers introduced by BindingDecl simply as local scalar variables? I imagine that all we need to develop is a schema for deriving entity name for BindingDecl. Is there anything else?
I don't want to represent entities for implicitly generated code in the summaries unless we have a pretty strong reason to do so.
For the pointer flow extractor, we should extend it to extract the flow from DecompositionDecl from the initializer expression to the entity introduced by BindingDecl.
At this point and as part of this problem, I don't want to generalize the pointer flow graph.