Skip to content

Commit 012680f

Browse files
authored
[clang][Expr] Teach IgnoreUnlessSpelledInSource about implicit calls to std::get free function (#122265)
When we generate the debug-info for a `VarDecl` we try to determine whether it was introduced as part of a structure binding (aka a "holding var"). If it was then we don't mark it as `artificial`. The heuristic to determine a holding var uses `IgnoreUnlessSpelledInSource` to unwrap the `VarDecl` initializer until we hit a `DeclRefExpr` that refers to a `Decomposition`. For "tuple-like decompositions", Clang will generate a call to a `template<size_t I> Foo get(Bar)` function that retrieves the `Ith` element from the tuple-like structure. If that function is a member function, we get an AST that looks as follows: ``` VarDecl implicit used z1 'std::tuple_element<0, B>::type &&' cinit `-ExprWithCleanups <col:10> 'int' xvalue `-MaterializeTemporaryExpr <col:10> 'int' xvalue extended by Var 0x11d110cf8 'z1' 'std::tuple_element<0, B>::type &&' `-CXXMemberCallExpr <col:10> 'int' `-MemberExpr <col:10> '<bound member function type>' .get 0x11d104390 `-ImplicitCastExpr <col:10> 'B' xvalue <NoOp> `-DeclRefExpr <col:10> 'B' lvalue Decomposition 0x11d1100a8 '' 'B' ``` `IgnoreUnlessSpelledInSource` happily unwraps this down to the `DeclRefExpr`. However, when the `get` helper is a free function (which it is for `std::pair` in libc++ for example), then the AST is: ``` VarDecl col:16 implicit used k 'std::tuple_element<0, const std::tuple<int, int>>::type &' cinit `-CallExpr <col:16> 'const typename tuple_element<0UL, tuple<int, int>>::type':'const int' lvalue adl |-ImplicitCastExpr <col:16> 'const typename tuple_element<0UL, tuple<int, int>>::type &(*)(const tuple<int, int> &) noexcept' <FunctionToPointerDecay> | `-DeclRefExpr <col:16> 'const typename tuple_element<0UL, tuple<int, int>>::type &(const tuple<int, int> &) noexcept' lvalue Function 0x1210262d8 'get' 'const typename tuple_element<0UL, tuple<int, int>>::type &(const tuple<int, int> &) noexcept' (FunctionTemplate 0x11d068088 'get') `-DeclRefExpr <col:16> 'const std::tuple<int, int>' lvalue Decomposition 0x121021518 '' 'const std::tuple<int, int> &' ``` `IgnoreUnlessSpelledInSource` doesn't unwrap this `CallExpr`, so we incorrectly mark the binding as `artificial` in debug-info. This patch adjusts `IgnoreUnlessSpelledInSource` so it unwraps implicit `CallExpr`s. It's almost identical to how we treat implicit constructor calls (unfortunately the code can't quite be re-used because a `CXXConstructExpr` is-not a `CallExpr`, and we check `isElidable`, which doesn't exist for regular function calls. So I added a new `IgnoreImplicitCallSingleStep`). Fixes #122028
1 parent c1d838e commit 012680f

File tree

5 files changed

+267
-8
lines changed

5 files changed

+267
-8
lines changed

clang/lib/AST/Expr.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,18 @@ Stmt *BlockExpr::getBody() {
25452545
// Generic Expression Routines
25462546
//===----------------------------------------------------------------------===//
25472547

2548+
/// Helper to determine wether \c E is a CXXConstructExpr constructing
2549+
/// a DecompositionDecl. Used to skip Clang-generated calls to std::get
2550+
/// for structured bindings.
2551+
static bool IsDecompositionDeclRefExpr(const Expr *E) {
2552+
const auto *Unwrapped = E->IgnoreUnlessSpelledInSource();
2553+
const auto *Ref = dyn_cast<DeclRefExpr>(Unwrapped);
2554+
if (!Ref)
2555+
return false;
2556+
2557+
return isa_and_nonnull<DecompositionDecl>(Ref->getDecl());
2558+
}
2559+
25482560
bool Expr::isReadIfDiscardedInCPlusPlus11() const {
25492561
// In C++11, discarded-value expressions of a certain form are special,
25502562
// according to [expr]p10:
@@ -3159,10 +3171,39 @@ Expr *Expr::IgnoreUnlessSpelledInSource() {
31593171
}
31603172
return E;
31613173
};
3174+
3175+
// Used when Clang generates calls to std::get for decomposing
3176+
// structured bindings.
3177+
auto IgnoreImplicitCallSingleStep = [](Expr *E) {
3178+
auto *C = dyn_cast<CallExpr>(E);
3179+
if (!C)
3180+
return E;
3181+
3182+
// Looking for calls to a std::get, which usually just takes
3183+
// 1 argument (i.e., the structure being decomposed). If it has
3184+
// more than 1 argument, the others need to be defaulted.
3185+
unsigned NumArgs = C->getNumArgs();
3186+
if (NumArgs == 0 || (NumArgs > 1 && !isa<CXXDefaultArgExpr>(C->getArg(1))))
3187+
return E;
3188+
3189+
Expr *A = C->getArg(0);
3190+
3191+
// This was spelled out in source. Don't ignore.
3192+
if (A->getSourceRange() != E->getSourceRange())
3193+
return E;
3194+
3195+
// If the argument refers to a DecompositionDecl construction,
3196+
// ignore it.
3197+
if (IsDecompositionDeclRefExpr(A))
3198+
return A;
3199+
3200+
return E;
3201+
};
3202+
31623203
return IgnoreExprNodes(
31633204
this, IgnoreImplicitSingleStep, IgnoreImplicitCastsExtraSingleStep,
31643205
IgnoreParensOnlySingleStep, IgnoreImplicitConstructorSingleStep,
3165-
IgnoreImplicitMemberCallSingleStep);
3206+
IgnoreImplicitMemberCallSingleStep, IgnoreImplicitCallSingleStep);
31663207
}
31673208

31683209
bool Expr::isDefaultArgument() const {

clang/test/DebugInfo/CXX/structured-binding.cpp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
// RUN: %clang_cc1 -emit-llvm -debug-info-kind=standalone -triple %itanium_abi_triple %s -o - | FileCheck %s --implicit-check-not="call void @llvm.dbg.declare"
1+
// RUN: %clang_cc1 -std=c++23 -emit-llvm -debug-info-kind=standalone -triple %itanium_abi_triple %s -o - | FileCheck %s --implicit-check-not="call void @llvm.dbg.declare"
22

3+
// CHECK: define {{.*}} i32 @_Z1fv
34
// CHECK: #dbg_declare(ptr %{{[a-z]+}}, ![[VAR_0:[0-9]+]], !DIExpression(),
45
// CHECK: #dbg_declare(ptr %{{[0-9]+}}, ![[VAR_1:[0-9]+]], !DIExpression(),
56
// CHECK: #dbg_declare(ptr %{{[0-9]+}}, ![[VAR_2:[0-9]+]], !DIExpression(DW_OP_plus_uconst, 4),
67
// CHECK: #dbg_declare(ptr %{{[0-9]+}}, ![[VAR_3:[0-9]+]], !DIExpression(DW_OP_deref),
78
// CHECK: #dbg_declare(ptr %{{[0-9]+}}, ![[VAR_4:[0-9]+]], !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 4),
89
// CHECK: #dbg_declare(ptr %z1, ![[VAR_5:[0-9]+]], !DIExpression()
910
// CHECK: #dbg_declare(ptr %z2, ![[VAR_6:[0-9]+]], !DIExpression()
11+
// CHECK: #dbg_declare(ptr %k, ![[VAR_7:[0-9]+]], !DIExpression()
12+
// CHECK: #dbg_declare(ptr %v, ![[VAR_8:[0-9]+]], !DIExpression()
13+
// CHECK: #dbg_declare(ptr %w, ![[VAR_9:[0-9]+]], !DIExpression()
14+
// CHECK: #dbg_declare(ptr %m, ![[VAR_10:[0-9]+]], !DIExpression()
15+
// CHECK: #dbg_declare(ptr %n, ![[VAR_11:[0-9]+]], !DIExpression()
16+
// CHECK: #dbg_declare(ptr %s, ![[VAR_12:[0-9]+]], !DIExpression()
17+
// CHECK: #dbg_declare(ptr %p, ![[VAR_13:[0-9]+]], !DIExpression()
1018
// CHECK: getelementptr inbounds nuw %struct.A, ptr {{.*}}, i32 0, i32 1, !dbg ![[Y1_DEBUG_LOC:[0-9]+]]
1119
// CHECK: getelementptr inbounds nuw %struct.A, ptr {{.*}}, i32 0, i32 1, !dbg ![[Y2_DEBUG_LOC:[0-9]+]]
1220
// CHECK: load ptr, ptr %z2, {{.*}}!dbg ![[Z2_DEBUG_LOC:[0-9]+]]
@@ -20,6 +28,13 @@
2028
// CHECK: ![[VAR_4]] = !DILocalVariable(name: "y2", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
2129
// CHECK: ![[VAR_5]] = !DILocalVariable(name: "z1", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
2230
// CHECK: ![[VAR_6]] = !DILocalVariable(name: "z2", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
31+
// CHECK: ![[VAR_7]] = !DILocalVariable(name: "k", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
32+
// CHECK: ![[VAR_8]] = !DILocalVariable(name: "v", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
33+
// CHECK: ![[VAR_9]] = !DILocalVariable(name: "w", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
34+
// CHECK: ![[VAR_10]] = !DILocalVariable(name: "m", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
35+
// CHECK: ![[VAR_11]] = !DILocalVariable(name: "n", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
36+
// CHECK: ![[VAR_12]] = !DILocalVariable(name: "s", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
37+
// CHECK: ![[VAR_13]] = !DILocalVariable(name: "p", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}})
2338

2439
struct A {
2540
int x;
@@ -34,6 +49,22 @@ struct B {
3449
template<> int get<1>() { return z; }
3550
};
3651

52+
struct C {
53+
int w;
54+
int z;
55+
template<int> int get(this C&& self);
56+
template<> int get<0>(this C&& self) { return self.w; }
57+
template<> int get<1>(this C&& self) { return self.z; }
58+
};
59+
60+
struct D {
61+
int w;
62+
int z;
63+
template<int> int get(int unused = 0);
64+
template<> int get<0>(int unused) { return w; }
65+
template<> int get<1>(int unused) { return z; }
66+
};
67+
3768
// Note: the following declarations are necessary for decomposition of tuple-like
3869
// structured bindings
3970
namespace std {
@@ -44,7 +75,35 @@ struct tuple_size<B> {
4475
static constexpr unsigned value = 2;
4576
};
4677

78+
template<>
79+
struct tuple_size<C> {
80+
static constexpr unsigned value = 2;
81+
};
82+
83+
template<>
84+
struct tuple_size<D> {
85+
static constexpr unsigned value = 2;
86+
};
87+
4788
template<unsigned, typename T> struct tuple_element { using type = int; };
89+
90+
// Decomposition of tuple-like bindings but where the `get` methods
91+
// are declared as free functions.
92+
struct triple {
93+
int k;
94+
int v;
95+
int w;
96+
};
97+
98+
template<>
99+
struct tuple_size<triple> {
100+
static constexpr unsigned value = 3;
101+
};
102+
103+
template <unsigned I> int get(triple);
104+
template <> int get<0>(triple p) { return p.k; }
105+
template <> int get<1>(triple p) { return p.v; }
106+
template <> int get<2>(triple p) { return p.w; }
48107
} // namespace std
49108

50109
int f() {
@@ -58,6 +117,9 @@ int f() {
58117
auto &[c1, c2] = cmplx;
59118
int vctr __attribute__ ((vector_size (sizeof(int)*2)))= {1, 2};
60119
auto &[v1, v2] = vctr;
120+
auto [k, v, w] = std::triple{3, 4, 5};
121+
auto [m, n] = C{2, 3};
122+
auto [s, p] = D{2, 3};
61123
return //
62124
x1 //
63125
+ //

clang/unittests/AST/ASTTraverserTest.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,12 @@ struct Pair
11741174
int x, y;
11751175
};
11761176
1177+
// Tuple-like structure with a `get` method that has a default argument.
1178+
struct Pair2
1179+
{
1180+
int x, y;
1181+
};
1182+
11771183
// Note: these utilities are required to force binding to tuple like structure
11781184
namespace std
11791185
{
@@ -1188,6 +1194,12 @@ namespace std
11881194
static constexpr size_t value = 2;
11891195
};
11901196
1197+
template <>
1198+
struct tuple_size<Pair2>
1199+
{
1200+
static constexpr size_t value = 2;
1201+
};
1202+
11911203
template <size_t I, class T>
11921204
struct tuple_element
11931205
{
@@ -1199,12 +1211,17 @@ namespace std
11991211
template <size_t I>
12001212
int &&get(Pair &&p);
12011213
1214+
template <size_t I>
1215+
int &&get(Pair2 &&p, int unused = 0);
1216+
12021217
void decompTuple()
12031218
{
12041219
Pair p{1, 2};
12051220
auto [a, b] = p;
12061221
12071222
a = 3;
1223+
1224+
auto [c, d] = Pair2{3, 4};
12081225
}
12091226
12101227
)cpp",
@@ -1586,6 +1603,62 @@ DecompositionDecl ''
15861603
|-DeclRefExpr 'p'
15871604
|-BindingDecl 'a'
15881605
`-BindingDecl 'b'
1606+
)cpp");
1607+
}
1608+
1609+
{
1610+
auto FN = ast_matchers::match(
1611+
functionDecl(hasName("decompTuple"),
1612+
hasDescendant(callExpr(hasAncestor(varDecl(
1613+
hasName("a"),
1614+
hasAncestor(bindingDecl()))))
1615+
.bind("decomp_call"))),
1616+
AST2->getASTContext());
1617+
EXPECT_EQ(FN.size(), 1u);
1618+
1619+
EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<CallExpr>("decomp_call")),
1620+
R"cpp(
1621+
CallExpr
1622+
|-ImplicitCastExpr
1623+
| `-DeclRefExpr 'get'
1624+
`-ImplicitCastExpr
1625+
`-DeclRefExpr ''
1626+
)cpp");
1627+
1628+
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
1629+
FN[0].getNodeAs<CallExpr>("decomp_call")),
1630+
R"cpp(
1631+
DeclRefExpr ''
1632+
)cpp");
1633+
}
1634+
1635+
{
1636+
auto FN = ast_matchers::match(
1637+
functionDecl(hasName("decompTuple"),
1638+
hasDescendant(callExpr(hasAncestor(varDecl(
1639+
hasName("c"),
1640+
hasAncestor(bindingDecl()))))
1641+
.bind("decomp_call_with_default"))),
1642+
AST2->getASTContext());
1643+
EXPECT_EQ(FN.size(), 1u);
1644+
1645+
EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<CallExpr>(
1646+
"decomp_call_with_default")),
1647+
R"cpp(
1648+
CallExpr
1649+
|-ImplicitCastExpr
1650+
| `-DeclRefExpr 'get'
1651+
|-ImplicitCastExpr
1652+
| `-DeclRefExpr ''
1653+
`-CXXDefaultArgExpr
1654+
`-IntegerLiteral
1655+
)cpp");
1656+
1657+
EXPECT_EQ(
1658+
dumpASTString(TK_IgnoreUnlessSpelledInSource,
1659+
FN[0].getNodeAs<CallExpr>("decomp_call_with_default")),
1660+
R"cpp(
1661+
DeclRefExpr ''
15891662
)cpp");
15901663
}
15911664
}

lldb/test/API/lang/cpp/structured-binding/TestStructuredBinding.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,17 @@ def test(self):
9898
self.expect_expr("tx2", result_value="4")
9999
self.expect_expr("ty2", result_value="'z'")
100100
self.expect_expr("tz2", result_value="10")
101+
102+
self.expect(
103+
"frame variable",
104+
substrs=[
105+
"tx1 =",
106+
"ty1 =",
107+
"tz1 =",
108+
"tx2 =",
109+
"ty2 =",
110+
"tz2 =",
111+
"mp1 =",
112+
"mp2 =",
113+
],
114+
)

lldb/test/API/lang/cpp/structured-binding/main.cpp

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,80 @@
11
// Structured binding in C++ can bind identifiers to subobjects of an object.
22
//
3-
// There are three cases we need to test:
3+
// There are four cases we need to test:
44
// 1) arrays
5-
// 2) tuples like objects
6-
// 3) non-static data members
5+
// 2) tuple-like objects with `get` member functions
6+
// 3) tuple-like objects with `get` free functions
7+
// 4) non-static data members
78
//
89
// They can also bind by copy, reference or rvalue reference.
910

10-
#include <tuple>
11+
struct MyPair {
12+
int m1;
13+
int m2;
14+
15+
// Helpers to enable tuple-like decomposition.
16+
template <unsigned> int get();
17+
template <> int get<0>() { return m1; }
18+
template <> int get<1>() { return m2; }
19+
};
20+
21+
namespace std {
22+
template <typename T1, typename T2, typename T3> struct mock_tuple {
23+
T1 m1;
24+
T2 m2;
25+
T3 m3;
26+
};
27+
28+
template <typename T> struct tuple_size;
29+
30+
template <unsigned, typename T> struct tuple_element;
31+
32+
// Helpers to enable tuple-like decomposition for MyPair
33+
template <unsigned I> struct tuple_element<I, MyPair> {
34+
using type = int;
35+
};
36+
37+
template <> struct tuple_size<MyPair> {
38+
static constexpr unsigned value = 2;
39+
};
40+
41+
// Helpers to enable tuple-like decomposition for mock_tuple
42+
template <typename T1, typename T2, typename T3>
43+
struct tuple_element<0, mock_tuple<T1, T2, T3>> {
44+
using type = T1;
45+
};
46+
47+
template <typename T1, typename T2, typename T3>
48+
struct tuple_element<1, mock_tuple<T1, T2, T3>> {
49+
using type = T2;
50+
};
51+
52+
template <typename T1, typename T2, typename T3>
53+
struct tuple_element<2, mock_tuple<T1, T2, T3>> {
54+
using type = T3;
55+
};
56+
57+
template <typename T1, typename T2, typename T3>
58+
struct tuple_size<mock_tuple<T1, T2, T3>> {
59+
static constexpr unsigned value = 3;
60+
};
61+
62+
template <unsigned I, typename T1, typename T2, typename T3>
63+
typename tuple_element<I, mock_tuple<T1, T2, T3>>::type
64+
get(mock_tuple<T1, T2, T3> p) {
65+
switch (I) {
66+
case 0:
67+
return p.m1;
68+
case 1:
69+
return p.m2;
70+
case 2:
71+
return p.m3;
72+
default:
73+
__builtin_trap();
74+
}
75+
}
76+
77+
} // namespace std
1178

1279
struct A {
1380
int x;
@@ -54,16 +121,18 @@ int main() {
54121
char y{'z'};
55122
int z{10};
56123

57-
std::tuple<float, char, int> tpl(x, y, z);
124+
std::mock_tuple<float, char, int> tpl{.m1 = x, .m2 = y, .m3 = z};
58125
auto [tx1, ty1, tz1] = tpl;
59126
auto &[tx2, ty2, tz2] = tpl;
60127

128+
auto [mp1, mp2] = MyPair{.m1 = 1, .m2 = 2};
129+
61130
return a1.x + b1 + c1 + d1 + e1 + f1 + a2.y + b2 + c2 + d2 + e2 + f2 + a3.x +
62131
b3 + c3 + d3 + e3 + f3 + carr_copy1 + carr_copy2 + carr_copy3 +
63132
sarr_copy1 + sarr_copy2 + sarr_copy3 + iarr_copy1 + iarr_copy2 +
64133
iarr_copy3 + carr_ref1 + carr_ref2 + carr_ref3 + sarr_ref1 +
65134
sarr_ref2 + sarr_ref3 + iarr_ref1 + iarr_ref2 + iarr_ref3 +
66135
carr_rref1 + carr_rref2 + carr_rref3 + sarr_rref1 + sarr_rref2 +
67136
sarr_rref3 + iarr_rref1 + iarr_rref2 + iarr_rref3 + tx1 + ty1 + tz1 +
68-
tx2 + ty2 + tz2; // break here
137+
tx2 + ty2 + tz2 + mp1 + mp2; // break here
69138
}

0 commit comments

Comments
 (0)