Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lldb][SBAPI] Add new SBType::GetTemplateParameterValue API #126901

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ class LLDB_API SBTarget {
friend class SBSection;
friend class SBSourceManager;
friend class SBSymbol;
friend class SBType;
Copy link
Member Author

@Michael137 Michael137 Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was only necessary to get access to SBTarget::GetSP

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is completely optional, it just seemed like a good opportunity to advertise my idea for avoiding these random criss-cross friend declarations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sorry, this is my bad. I didn't mean to imply that it should be done in this patch. I do like this idea, but I think that if we do that, it should be done in a more wider fashion rather than a one-off.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah fair enough, reverted that for now

friend class SBTypeStaticField;
friend class SBValue;
friend class SBVariablesOptions;
Expand Down
7 changes: 7 additions & 0 deletions lldb/include/lldb/API/SBType.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ class SBType {

lldb::SBType GetTemplateArgumentType(uint32_t idx);

/// Returns the value of the non-type template parameter at index \c idx.
/// If \c idx is out-of-bounds or the template parameter doesn't have
/// a value, returns an empty SBValue.
///
/// This function will expand parameter packs.
lldb::SBValue GetTemplateArgumentValue(lldb::SBTarget target, uint32_t idx);

/// Return the TemplateArgumentKind of the template argument at index idx.
/// Variadic argument packs are automatically expanded.
lldb::TemplateArgumentKind GetTemplateArgumentKind(uint32_t idx);
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ class LLDB_API SBValue {
friend class SBModule;
friend class SBTarget;
friend class SBThread;
friend class SBType;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was necessary to get access to the SBValue constructor

friend class SBTypeStaticField;
friend class SBTypeSummary;
friend class SBValueList;
Expand Down
34 changes: 34 additions & 0 deletions lldb/source/API/SBType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,40 @@ lldb::TemplateArgumentKind SBType::GetTemplateArgumentKind(uint32_t idx) {
return eTemplateArgumentKindNull;
}

lldb::SBValue SBType::GetTemplateArgumentValue(lldb::SBTarget target,
uint32_t idx) {
LLDB_INSTRUMENT_VA(this, target, idx);

if (!IsValid())
return SBValue();

std::optional<CompilerType::IntegralTemplateArgument> arg;
const bool expand_pack = true;
switch (GetTemplateArgumentKind(idx)) {
case eTemplateArgumentKindIntegral:
arg = m_opaque_sp->GetCompilerType(false).GetIntegralTemplateArgument(
idx, expand_pack);
break;
default:
break;
}

if (!arg)
return {};

Scalar value{arg->value};

if (!value.IsValid())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this ever be not valid? What would that even mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea we don't need this

return {};

DataExtractor data;
value.GetData(data);

auto value_obj_sp = ValueObjectConstResult::Create(
target.GetSP().get(), arg->type, ConstString("value"), data);
return SBValue(std::move(value_obj_sp));
}

SBType SBType::FindDirectNestedType(const char *name) {
LLDB_INSTRUMENT_VA(this, name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class TemplatePackArgsTestCase(TestBase):
def test_template_argument_pack(self):
self.build()
(_, _, thread, _) = lldbutil.run_to_source_breakpoint(
(target, _, thread, _) = lldbutil.run_to_source_breakpoint(
self, "breakpoint here", lldb.SBFileSpec("main.cpp"), exe_name="a.out"
)
frame = thread.GetSelectedFrame()
Expand All @@ -33,10 +33,25 @@ def test_template_argument_pack(self):
self.assertEqual(
only_pack.GetType().GetTemplateArgumentType(2).GetName(), "double"
)
# Access the C<double, 42> template parameter.

nested_template = only_pack.GetType().GetTemplateArgumentType(3)
self.assertEqual(nested_template.GetName(), "D<int, int, bool>")
self.assertEqual(nested_template.GetNumberOfTemplateArguments(), 3)
self.assertEqual(nested_template.GetTemplateArgumentType(0).GetName(), "int")
self.assertEqual(nested_template.GetTemplateArgumentType(1).GetName(), "int")
self.assertEqual(nested_template.GetTemplateArgumentType(2).GetName(), "bool")

my_c = frame.FindVariable("myC")
self.assertTrue(my_c.IsValid(), "make sure we find the myC variable")

# Out of bounds index.
self.assertFalse(my_c.GetType().GetTemplateArgumentValue(target, 3))

# Out of bounds index.
template_param_value = my_c.GetType().GetTemplateArgumentValue(target, 1)
self.assertEqual(template_param_value.GetTypeName(), "int")
self.assertEqual(template_param_value.GetValueAsSigned(), 16)

template_param_value = my_c.GetType().GetTemplateArgumentValue(target, 2)
self.assertEqual(template_param_value.GetTypeName(), "int")
self.assertEqual(template_param_value.GetValueAsSigned(), 32)
1 change: 1 addition & 0 deletions lldb/test/API/lang/cpp/template-arguments/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CXX_SOURCES := main.cpp
CXXFLAGS_EXTRAS := -std=c++20

include Makefile.rules
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -27,10 +27,38 @@ def test(self):
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>")
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)
5 changes: 5 additions & 0 deletions lldb/test/API/lang/cpp/template-arguments/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Loading