Skip to content

Commit 9ebe6f9

Browse files
authored
[lldb][test] Fix libstdc++ std::variant formatter tests for valueless variants (#147283)
A default-constructed variant has a valid index (being the first element of the variant). The only case where the index is variant_npos is when the variant is "valueless", which [according to cppreference](https://en.cppreference.com/w/cpp/utility/variant/valueless_by_exception.html) only happens when an exception is thrown during assignment to the variant. I adjusted the test to test that scenario, and the formatter seems to work. Unblocks #147253
1 parent 16dad11 commit 9ebe6f9

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/TestDataFormatterLibStdcxxVariant.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Test lldb data formatter for LibStdC++ std::variant.
33
"""
44

5-
65
import lldb
76
from lldbsuite.test.decorators import *
87
from lldbsuite.test.lldbtest import *
@@ -62,17 +61,13 @@ def test_with_run_command(self):
6261
"frame variable v3",
6362
substrs=["v3 = Active Type = char {", "Value = 'A'", "}"],
6463
)
65-
"""
66-
TODO: temporarily disable No Value tests as they seem to fail on ubuntu/debian
67-
bots. Pending reproduce and investigation.
6864

69-
self.expect("frame variable v_no_value", substrs=["v_no_value = No Value"])
65+
self.expect("frame variable v_valueless", substrs=["v_valueless = No Value"])
7066

7167
self.expect(
72-
"frame variable v_many_types_no_value",
73-
substrs=["v_many_types_no_value = No Value"],
68+
"frame variable v_many_types_valueless",
69+
substrs=["v_many_types_valueless = No Value"],
7470
)
75-
"""
7671

7772
@add_test_categories(["libstdcxx"])
7873
def test_invalid_variant_index(self):

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/main.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include <vector>
55

66
struct S {
7-
operator int() { throw 42; }
7+
S() = default;
8+
S(S &&) { throw 42; }
9+
S &operator=(S &&) = default;
810
};
911

1012
int main() {
@@ -21,7 +23,7 @@ int main() {
2123
std::variant<int, double, char> v2;
2224
std::variant<int, double, char> v3;
2325
std::variant<std::variant<int, double, char>> v_v1;
24-
std::variant<int, double, char> v_no_value;
26+
std::variant<int, char, S> v_valueless = 5;
2527
// The next variant has many types, meaning the type index does not fit in
2628
// a byte and must be `unsigned short` instead of `unsigned char` when
2729
// using the unstable libc++ ABI. With stable libc++ ABI, the type index
@@ -43,8 +45,11 @@ int main() {
4345
int, int, int, int, int, int, int, int, int, int, int, int, int, int, int,
4446
int, int, int, int, int, int, int, int, int, int, int, int, int, int, int,
4547
int, int, int, int, int, int, int, int, int, int, int, int, int, int, int,
46-
int, int, int, int, int, int, int, int, int, int, int, int>
47-
v_many_types_no_value;
48+
int, int, int, int, int, int, int, int, int, int, int, int, S>
49+
v_many_types_valueless;
50+
51+
v_valueless = 5;
52+
v_many_types_valueless.emplace<0>(10);
4853

4954
v1 = 12; // v contains int
5055
v1_typedef = v1;
@@ -67,18 +72,22 @@ int main() {
6772
printf("%f\n", d); // break here
6873

6974
try {
70-
v_no_value.emplace<0>(S());
75+
// Exception in type-changing move-assignment is guaranteed to put
76+
// std::variant into a valueless state.
77+
v_valueless = S();
7178
} catch (...) {
7279
}
7380

74-
printf("%zu\n", v_no_value.index());
81+
printf("%d\n", v_valueless.valueless_by_exception());
7582

7683
try {
77-
v_many_types_no_value.emplace<0>(S());
84+
// Exception in move-assignment is guaranteed to put std::variant into a
85+
// valueless state.
86+
v_many_types_valueless = S();
7887
} catch (...) {
7988
}
8089

81-
printf("%zu\n", v_many_types_no_value.index());
90+
printf("%d\n", v_many_types_valueless.valueless_by_exception());
8291

8392
return 0; // break here
8493
}

0 commit comments

Comments
 (0)