Skip to content

Commit caf2767

Browse files
committed
[Clang][AST] Fixed BindingDecl AST-dump for tuple like structures
The AST of a BindingDecl in case of tuple like structures wasn't properly printed. For these bidnings there is information stored in BindingDecl::getHoldingVar(), and this information was't printed in the AST-dump. Differential Revision: https://reviews.llvm.org/D126131
1 parent 6226e46 commit caf2767

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

clang/include/clang/AST/ASTNodeTraverser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ class ASTNodeTraverser
467467
void VisitBindingDecl(const BindingDecl *D) {
468468
if (Traversal == TK_IgnoreUnlessSpelledInSource)
469469
return;
470+
471+
if (const auto *V = D->getHoldingVar())
472+
Visit(V);
473+
470474
if (const auto *E = D->getBinding())
471475
Visit(E);
472476
}

clang/unittests/AST/ASTTraverserTest.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,46 @@ void decomposition()
11571157
f = 42;
11581158
}
11591159
1160+
typedef __typeof(sizeof(int)) size_t;
1161+
1162+
struct Pair
1163+
{
1164+
int x, y;
1165+
};
1166+
1167+
// Note: these utilities are required to force binding to tuple like structure
1168+
namespace std
1169+
{
1170+
template <typename E>
1171+
struct tuple_size
1172+
{
1173+
};
1174+
1175+
template <>
1176+
struct tuple_size<Pair>
1177+
{
1178+
static constexpr size_t value = 2;
1179+
};
1180+
1181+
template <size_t I, class T>
1182+
struct tuple_element
1183+
{
1184+
using type = int;
1185+
};
1186+
1187+
};
1188+
1189+
template <size_t I>
1190+
int &&get(Pair &&p);
1191+
1192+
void decompTuple()
1193+
{
1194+
Pair p{1, 2};
1195+
auto [a, b] = p;
1196+
1197+
a = 3;
1198+
}
1199+
11601200
)cpp",
11611201
{"-std=c++20"});
11621202

@@ -1492,6 +1532,48 @@ DecompositionDecl ''
14921532
|-BindingDecl 'f'
14931533
|-BindingDecl 's'
14941534
`-BindingDecl 't'
1535+
)cpp");
1536+
}
1537+
1538+
{
1539+
auto FN = ast_matchers::match(
1540+
functionDecl(hasName("decompTuple"),
1541+
hasDescendant(decompositionDecl().bind("decomp"))),
1542+
AST2->getASTContext());
1543+
EXPECT_EQ(FN.size(), 1u);
1544+
1545+
EXPECT_EQ(
1546+
dumpASTString(TK_AsIs, FN[0].getNodeAs<DecompositionDecl>("decomp")),
1547+
R"cpp(
1548+
DecompositionDecl ''
1549+
|-CXXConstructExpr
1550+
| `-ImplicitCastExpr
1551+
| `-DeclRefExpr 'p'
1552+
|-BindingDecl 'a'
1553+
| |-VarDecl 'a'
1554+
| | `-CallExpr
1555+
| | |-ImplicitCastExpr
1556+
| | | `-DeclRefExpr 'get'
1557+
| | `-ImplicitCastExpr
1558+
| | `-DeclRefExpr ''
1559+
| `-DeclRefExpr 'a'
1560+
`-BindingDecl 'b'
1561+
|-VarDecl 'b'
1562+
| `-CallExpr
1563+
| |-ImplicitCastExpr
1564+
| | `-DeclRefExpr 'get'
1565+
| `-ImplicitCastExpr
1566+
| `-DeclRefExpr ''
1567+
`-DeclRefExpr 'b'
1568+
)cpp");
1569+
1570+
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
1571+
FN[0].getNodeAs<DecompositionDecl>("decomp")),
1572+
R"cpp(
1573+
DecompositionDecl ''
1574+
|-DeclRefExpr 'p'
1575+
|-BindingDecl 'a'
1576+
`-BindingDecl 'b'
14951577
)cpp");
14961578
}
14971579
}

0 commit comments

Comments
 (0)