diff options
| author | Michael Buch <michaelbuch12@gmail.com> | 2025-02-13 09:53:17 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-13 09:53:17 +0000 |
| commit | 3ad8657ff60b9967235ad65fdb8b767aae0e799d (patch) | |
| tree | 3d201c457986b9c6949e05c61017590f909b3302 /lldb/test/API/lang/cpp/template-arguments | |
| parent | 48897774a5accdd33fcb6d7a7b698a61c96006ed (diff) | |
[lldb][SBAPI] Add new SBType::GetTemplateParameterValue API (#126901)
This patch adds a new API to `SBType` to retrieve the value of a
template parameter given an index. We re-use the
`TypeSystemClang::GetIntegralTemplateArgument` for this and thus
currently only supports integral non-type template parameters. Types
like float/double are not supported yet.
rdar://144395216
Diffstat (limited to 'lldb/test/API/lang/cpp/template-arguments')
3 files changed, 40 insertions, 2 deletions
diff --git a/lldb/test/API/lang/cpp/template-arguments/Makefile b/lldb/test/API/lang/cpp/template-arguments/Makefile index 99998b20bcb0..4f79c0a900c3 100644 --- a/lldb/test/API/lang/cpp/template-arguments/Makefile +++ b/lldb/test/API/lang/cpp/template-arguments/Makefile @@ -1,3 +1,4 @@ CXX_SOURCES := main.cpp +CXXFLAGS_EXTRAS := -std=c++20 include Makefile.rules diff --git a/lldb/test/API/lang/cpp/template-arguments/TestCppTemplateArguments.py b/lldb/test/API/lang/cpp/template-arguments/TestCppTemplateArguments.py index 7b63a6cca8db..f1b3d7a9806f 100644 --- a/lldb/test/API/lang/cpp/template-arguments/TestCppTemplateArguments.py +++ b/lldb/test/API/lang/cpp/template-arguments/TestCppTemplateArguments.py @@ -8,7 +8,7 @@ class TestCase(TestBase): @no_debug_info_test def test(self): self.build() - self.dbg.CreateTarget(self.getBuildArtifact("a.out")) + target = self.dbg.CreateTarget(self.getBuildArtifact("a.out")) value = self.expect_expr("temp1", result_type="C<int, 2>") template_type = value.GetType() @@ -27,10 +27,42 @@ class TestCase(TestBase): self.assertEqual( template_type.GetTemplateArgumentType(1).GetName(), "unsigned int" ) - # FIXME: There is no way to get the actual value of the parameter. + + # Template parameter isn't a NTTP. + self.assertFalse(template_type.GetTemplateArgumentValue(target, 0)) + + # Template parameter index out-of-bounds. + self.assertFalse(template_type.GetTemplateArgumentValue(target, 2)) + + # Template parameter is a NTTP. + param_val = template_type.GetTemplateArgumentValue(target, 1) + self.assertEqual(param_val.GetTypeName(), "unsigned int") + self.assertEqual(param_val.GetValueAsUnsigned(), 2) # Try to get an invalid template argument. self.assertEqual( template_type.GetTemplateArgumentKind(2), lldb.eTemplateArgumentKindNull ) self.assertEqual(template_type.GetTemplateArgumentType(2).GetName(), "") + + value = self.expect_expr("temp2", result_type="Foo<short, -2>") + + # Can't get template parameter value with invalid target. + self.assertFalse(value.GetType().GetTemplateArgumentValue(lldb.SBTarget(), 1)) + + template_param_value = value.GetType().GetTemplateArgumentValue(target, 1) + self.assertTrue(template_param_value) + self.assertEqual(template_param_value.GetTypeName(), "short") + self.assertEqual(template_param_value.GetValueAsSigned(), -2) + + value = self.expect_expr("temp3", result_type="Foo<char, 'v'>") + template_param_value = value.GetType().GetTemplateArgumentValue(target, 1) + self.assertTrue(template_param_value) + self.assertEqual(template_param_value.GetTypeName(), "char") + self.assertEqual(chr(template_param_value.GetValueAsSigned()), "v") + + # FIXME: type should be Foo<float, 2.0f> + # FIXME: double/float NTTP parameter values currently not supported. + value = self.expect_expr("temp4", result_type="Foo<float, float>") + template_param_value = value.GetType().GetTemplateArgumentValue(target, 1) + self.assertFalse(template_param_value) diff --git a/lldb/test/API/lang/cpp/template-arguments/main.cpp b/lldb/test/API/lang/cpp/template-arguments/main.cpp index 728bd400c258..0c0eb97cbc85 100644 --- a/lldb/test/API/lang/cpp/template-arguments/main.cpp +++ b/lldb/test/API/lang/cpp/template-arguments/main.cpp @@ -5,4 +5,9 @@ struct C { C<int, 2> temp1; +template <typename T, T value> struct Foo {}; +Foo<short, -2> temp2; +Foo<char, 'v'> temp3; +Foo<float, 2.0f> temp4; + int main() {} |
