[Lldb-commits] [lldb] 9ebe6f9 - [lldb][test] Fix libstdc++ std::variant formatter tests for valueless variants (#147283)
via lldb-commits
lldb-commits at lists.llvm.org
Mon Jul 7 06:28:22 PDT 2025
Author: Michael Buch
Date: 2025-07-07T14:28:19+01:00
New Revision: 9ebe6f9a1f9e3473166cd57282c9827df12416a3
URL: https://github.com/llvm/llvm-project/commit/9ebe6f9a1f9e3473166cd57282c9827df12416a3
DIFF: https://github.com/llvm/llvm-project/commit/9ebe6f9a1f9e3473166cd57282c9827df12416a3.diff
LOG: [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 https://github.com/llvm/llvm-project/pull/147253
Added:
Modified:
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/TestDataFormatterLibStdcxxVariant.py
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/main.cpp
Removed:
################################################################################
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/TestDataFormatterLibStdcxxVariant.py b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/TestDataFormatterLibStdcxxVariant.py
index 394e221809f7c..dae9b24fbbcfe 100644
--- a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/TestDataFormatterLibStdcxxVariant.py
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/TestDataFormatterLibStdcxxVariant.py
@@ -2,7 +2,6 @@
Test lldb data formatter for LibStdC++ std::variant.
"""
-
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
@@ -62,17 +61,13 @@ def test_with_run_command(self):
"frame variable v3",
substrs=["v3 = Active Type = char {", "Value = 'A'", "}"],
)
- """
- TODO: temporarily disable No Value tests as they seem to fail on ubuntu/debian
- bots. Pending reproduce and investigation.
- self.expect("frame variable v_no_value", substrs=["v_no_value = No Value"])
+ self.expect("frame variable v_valueless", substrs=["v_valueless = No Value"])
self.expect(
- "frame variable v_many_types_no_value",
- substrs=["v_many_types_no_value = No Value"],
+ "frame variable v_many_types_valueless",
+ substrs=["v_many_types_valueless = No Value"],
)
- """
@add_test_categories(["libstdcxx"])
def test_invalid_variant_index(self):
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/main.cpp b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/main.cpp
index 36e0f74f831f8..235928264add1 100644
--- a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/main.cpp
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/variant/main.cpp
@@ -4,7 +4,9 @@
#include <vector>
struct S {
- operator int() { throw 42; }
+ S() = default;
+ S(S &&) { throw 42; }
+ S &operator=(S &&) = default;
};
int main() {
@@ -21,7 +23,7 @@ int main() {
std::variant<int, double, char> v2;
std::variant<int, double, char> v3;
std::variant<std::variant<int, double, char>> v_v1;
- std::variant<int, double, char> v_no_value;
+ std::variant<int, char, S> v_valueless = 5;
// The next variant has many types, meaning the type index does not fit in
// a byte and must be `unsigned short` instead of `unsigned char` when
// using the unstable libc++ ABI. With stable libc++ ABI, the type index
@@ -43,8 +45,11 @@ int main() {
int, int, int, int, int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int, int, int, int, int,
- int, int, int, int, int, int, int, int, int, int, int, int>
- v_many_types_no_value;
+ int, int, int, int, int, int, int, int, int, int, int, int, S>
+ v_many_types_valueless;
+
+ v_valueless = 5;
+ v_many_types_valueless.emplace<0>(10);
v1 = 12; // v contains int
v1_typedef = v1;
@@ -67,18 +72,22 @@ int main() {
printf("%f\n", d); // break here
try {
- v_no_value.emplace<0>(S());
+ // Exception in type-changing move-assignment is guaranteed to put
+ // std::variant into a valueless state.
+ v_valueless = S();
} catch (...) {
}
- printf("%zu\n", v_no_value.index());
+ printf("%d\n", v_valueless.valueless_by_exception());
try {
- v_many_types_no_value.emplace<0>(S());
+ // Exception in move-assignment is guaranteed to put std::variant into a
+ // valueless state.
+ v_many_types_valueless = S();
} catch (...) {
}
- printf("%zu\n", v_many_types_no_value.index());
+ printf("%d\n", v_many_types_valueless.valueless_by_exception());
return 0; // break here
}
More information about the lldb-commits
mailing list