[Lldb-commits] [lldb] [lldb][test] Fix libstdc++ std::variant formatter for empty variant (PR #147283)
Michael Buch via lldb-commits
lldb-commits at lists.llvm.org
Mon Jul 7 05:53:05 PDT 2025
https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/147283
>From be3eb2431b0649ce2730a4cf832dc474e262f49e Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Mon, 7 Jul 2025 12:40:37 +0100
Subject: [PATCH 1/3] [lldb][test] Fix libstdc++ std::variant formatter for
empty variant
Instead of using the byte-size to make a guess at what the
`std::variant_npos` value is, just look it up in debug-info.
Unblocks https://github.com/llvm/llvm-project/pull/147253
---
lldb/examples/synthetic/gnu_libstdcpp.py | 13 ++++---------
.../variant/TestDataFormatterLibStdcxxVariant.py | 5 -----
.../data-formatter-stl/libstdcpp/variant/main.cpp | 1 +
3 files changed, 5 insertions(+), 14 deletions(-)
diff --git a/lldb/examples/synthetic/gnu_libstdcpp.py b/lldb/examples/synthetic/gnu_libstdcpp.py
index 20b9488af5597..96b11322db775 100644
--- a/lldb/examples/synthetic/gnu_libstdcpp.py
+++ b/lldb/examples/synthetic/gnu_libstdcpp.py
@@ -889,17 +889,12 @@ def VariantSummaryProvider(valobj, dict):
if not (index_obj and index_obj.IsValid() and data_obj and data_obj.IsValid()):
return "<Can't find _M_index or _M_u>"
- def get_variant_npos_value(index_byte_size):
- if index_byte_size == 1:
- return 0xFF
- elif index_byte_size == 2:
- return 0xFFFF
- else:
- return 0xFFFFFFFF
+ npos = valobj.GetTarget().FindFirstGlobalVariable("std::variant_npos")
+ if not npos:
+ return "<Can't find std::variant_npos sentinel>"
- npos_value = get_variant_npos_value(index_obj.GetByteSize())
index = index_obj.GetValueAsUnsigned(0)
- if index == npos_value:
+ if index == npos.GetValueAsUnsigned(0):
return " No Value"
# Strip references and typedefs.
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..c3325c9e73cb9 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,9 +61,6 @@ 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"])
@@ -72,7 +68,6 @@ def test_with_run_command(self):
"frame variable v_many_types_no_value",
substrs=["v_many_types_no_value = 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..acb07d5700b8a 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
@@ -49,6 +49,7 @@ int main() {
v1 = 12; // v contains int
v1_typedef = v1;
v_v1 = v1;
+ v1_typedef = v1;
int i = std::get<int>(v1);
printf("%d\n", i); // break here
>From 59518c7f14a6beeb29ebdc8b33eeede9176418b3 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Mon, 7 Jul 2025 12:47:28 +0100
Subject: [PATCH 2/3] fixup! revert redundant test change
---
.../data-formatter/data-formatter-stl/libstdcpp/variant/main.cpp | 1 -
1 file changed, 1 deletion(-)
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 acb07d5700b8a..36e0f74f831f8 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
@@ -49,7 +49,6 @@ int main() {
v1 = 12; // v contains int
v1_typedef = v1;
v_v1 = v1;
- v1_typedef = v1;
int i = std::get<int>(v1);
printf("%d\n", i); // break here
>From 788661c1c945b6a2b5190812a1b232f335096388 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Mon, 7 Jul 2025 13:52:44 +0100
Subject: [PATCH 3/3] fixup! fix test; revert changes to formatter
---
lldb/examples/synthetic/gnu_libstdcpp.py | 13 +++++++---
.../TestDataFormatterLibStdcxxVariant.py | 6 ++---
.../libstdcpp/variant/main.cpp | 25 +++++++++++++------
3 files changed, 29 insertions(+), 15 deletions(-)
diff --git a/lldb/examples/synthetic/gnu_libstdcpp.py b/lldb/examples/synthetic/gnu_libstdcpp.py
index 96b11322db775..20b9488af5597 100644
--- a/lldb/examples/synthetic/gnu_libstdcpp.py
+++ b/lldb/examples/synthetic/gnu_libstdcpp.py
@@ -889,12 +889,17 @@ def VariantSummaryProvider(valobj, dict):
if not (index_obj and index_obj.IsValid() and data_obj and data_obj.IsValid()):
return "<Can't find _M_index or _M_u>"
- npos = valobj.GetTarget().FindFirstGlobalVariable("std::variant_npos")
- if not npos:
- return "<Can't find std::variant_npos sentinel>"
+ def get_variant_npos_value(index_byte_size):
+ if index_byte_size == 1:
+ return 0xFF
+ elif index_byte_size == 2:
+ return 0xFFFF
+ else:
+ return 0xFFFFFFFF
+ npos_value = get_variant_npos_value(index_obj.GetByteSize())
index = index_obj.GetValueAsUnsigned(0)
- if index == npos.GetValueAsUnsigned(0):
+ if index == npos_value:
return " No Value"
# Strip references and typedefs.
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 c3325c9e73cb9..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
@@ -62,11 +62,11 @@ def test_with_run_command(self):
substrs=["v3 = Active Type = char {", "Value = 'A'", "}"],
)
- 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"])
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