Skip to content

[lldb] Return index of element in ValueObject path instead of the element's value #74413

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

Merged
merged 1 commit into from
Dec 13, 2023

Conversation

PortalPete
Copy link
Contributor

@PortalPete PortalPete commented Dec 5, 2023

It's more meaningful and actionable to indicate which element in the array has an issue by returning that element's index instead of its value. The value can be ambiguous if at least one other element has the same value.

The first parameter for these methods is idxs, an array of indices that represent a path from a (root) parent to on of its descendants, typically though intermediate descendants. When the path leads to a descendant that doesn't exist, the method is supposed to indicate where things went wrong by setting an index to &index_of_error, the second parameter.

The problem is the method sets *index_of_error to the index of the most recent parent's child in the hierarchy, which isn't very useful if there's more one index with the same value in the path.

In this example, each element in the path has a value that's the same as another element.

GetChildAtIndexPath({1, 2, 3, 3, 1, 1, 2}, &index_of_error);

Say the the second 1 in the path (the 5th element at [4]) doesn't exist and the code returns a nullptr. In that situation, the code sets *index_of_error to 1, but that's an ambiguous hint can implicate the 1st, 5th, or 6th element (at [0], [4], or [5]).

It’s more helpful to set *index_of_error to 4 to clearly indicate which element in idxs has the issue.

@PortalPete PortalPete marked this pull request as ready for review December 5, 2023 05:51
@llvmbot llvmbot added the lldb label Dec 5, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 5, 2023

@llvm/pr-subscribers-lldb

Author: Pete Lawrence (PortalPete)

Changes

rdar://119169160


Full diff: https://github.com/llvm/llvm-project/pull/74413.diff

1 Files Affected:

  • (modified) lldb/source/Core/ValueObject.cpp (+9-2)
diff --git a/lldb/source/Core/ValueObject.cpp b/lldb/source/Core/ValueObject.cpp
index a7f7ee64282d8..b13bffa0ca809 100644
--- a/lldb/source/Core/ValueObject.cpp
+++ b/lldb/source/Core/ValueObject.cpp
@@ -398,13 +398,16 @@ ValueObject::GetChildAtIndexPath(llvm::ArrayRef<size_t> idxs,
   if (idxs.size() == 0)
     return GetSP();
   ValueObjectSP root(GetSP());
+
+  size_t current_index = 0;
   for (size_t idx : idxs) {
     root = root->GetChildAtIndex(idx);
     if (!root) {
       if (index_of_error)
-        *index_of_error = idx;
+        *index_of_error = current_index;
       return root;
     }
+    current_index += 1;
   }
   return root;
 }
@@ -414,13 +417,17 @@ lldb::ValueObjectSP ValueObject::GetChildAtIndexPath(
   if (idxs.size() == 0)
     return GetSP();
   ValueObjectSP root(GetSP());
+
+  size_t current_index = 0;
   for (std::pair<size_t, bool> idx : idxs) {
     root = root->GetChildAtIndex(idx.first, idx.second);
     if (!root) {
       if (index_of_error)
-        *index_of_error = idx.first;
+        *index_of_error = current_index;
       return root;
     }
+
+    current_index += 1;
   }
   return root;
 }

@DavidSpickett
Copy link
Collaborator

I can see why the mistake was made, we have an index into a list of different indexes? Anyway, please add a test for this.

return root;
}
current_index += 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the tiniest nit on earth but current_index++ here to increment this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!
I had a hunch someone would point this one out, but I thought it'd be fun to see who, if anyone, would.
You passed the test! 😂

Personally, I prefer += 1 because:

  • Swift doesn't have the ++ operator.
  • In my mind += 1 more explicitly communicates my intent.
  • I've seen people get burned by the "post" part of a post-increment operator too many times. 🙃

Copy link
Contributor

Choose a reason for hiding this comment

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

After reading the Swift rationale for removing the ++ operator I understand why you'd use += 1 instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I take it you're referring to Swift evolution's proposal 0004 - Remove the ++ and -- operators.

As a coding and computer science mentor and writer, I naturally agree with Chris Lattner in that any code I write may serves as potential educational material to the reader/maintainer. For me, there are more cons than pros to the pre- and post-increment operators when I look at them through an educational lens.

@felipepiovezan
Copy link
Contributor

Should we change the documentation of these functions?

return root;
}

current_index += 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto for current_index++

Copy link
Contributor Author

@PortalPete PortalPete Dec 5, 2023

Choose a reason for hiding this comment

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

I believe I'll use the pre-increment operator ++current_index because that's what the LLVM coding style recommends.

Hard fast rule: Preincrement (++X) may be no slower than postincrement (X++) and could very well be a lot faster than it. Use preincrementation whenever possible.

...

Copy link
Contributor

@felipepiovezan felipepiovezan Dec 5, 2023

Choose a reason for hiding this comment

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

Note this guideline is about pre vs post (not about +=1), and it refers to iterators mostly.

I don't think you need to change anything here!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, but if we're not going with += 1 (for consistency and/or informal style?), then I prefer to use the pre-increment operator.

Or are you saying we should just stick with += 1 now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll just stick with += 1 and hope it becomes a Swift-inspired trend.

@PortalPete PortalPete force-pushed the misc/valueobject-index-path branch from 26891e6 to 5d99172 Compare December 6, 2023 00:10
@PortalPete
Copy link
Contributor Author

I can see why the mistake was made, we have an index into a list of different indexes? Anyway, please add a test for this.

I'm not really sure there's a good way to test this there doesn't appear to be anything that calls these methods, let alone an SB API.

CC @adrian-prantl

…ment's value

It's more meaningful and actionable to indicate which element in the array has an issue by returning that element's index instead of its value. The value can be ambiguous if at least one other element has the same value.

The first parameter for these methods is `idxs`, an array of indices that represent a path from a (root) parent to on of its descendants, typically though intermediate descendants. When the path leads to a descendant that doesn't exist, the method is supposed to indicate where things went wrong by setting an index to `&index_of_error`, the second parameter.

The problem is the method sets `*index_of_error` to the index of the most recent parent's child in the hierarchy, which isn't very useful if there's more one index with the same value in the path.

In this example, each element in the path has a value that's the same as another element.

```cpp
GetChildAtIndexPath({1, 2, 3, 3, 1, 1, 2}, &index_of_error);
```

Say the the second `1` in the path (the 5th element at `[4]`) doesn't exist and the code returns a `nullptr`. In that situation, the code sets `*index_of_error` to `1`, but that's an ambiguous hint can implicate the 1st, 5th, or 6th element (at `[0]`, `[4]`, or `[5]`).

It’s more helpful to set `*index_of_error` to `4` to clearly indicate which element in `idxs` has the issue.

rdar://119169160
@PortalPete PortalPete force-pushed the misc/valueobject-index-path branch from 5d99172 to 5731517 Compare December 8, 2023 00:43
@DavidSpickett
Copy link
Collaborator

doesn't appear to be anything that calls these methods

Candidate for removal then? If so it still makes sense to fix them, then remove them. Saves fixing them again if they are brought back.

Either way, I'm not going to block this by requiring a test case.

@adrian-prantl
Copy link
Collaborator

doesn't appear to be anything that calls these methods

Candidate for removal then? If so it still makes sense to fix them, then remove them. Saves fixing them again if they are brought back.

Either way, I'm not going to block this by requiring a test case.

I think that makes sense. @PortalPete why don't you land this without a test and then create a PR to remove the unused methods?

@PortalPete
Copy link
Contributor Author

doesn't appear to be anything that calls these methods

Candidate for removal then? If so it still makes sense to fix them, then remove them. Saves fixing them again if they are brought back.
Either way, I'm not going to block this by requiring a test case.

I think that makes sense. @PortalPete why don't you land this without a test and then create a PR to remove the unused methods?

That works for me, @adrian-prantl.
I'll remove these methods wholesale after someone approves/merges this PR on my behalf.

@adrian-prantl adrian-prantl merged commit c155269 into llvm:main Dec 13, 2023
adrian-prantl pushed a commit that referenced this pull request Dec 19, 2023
…t.cpp (#75870)

This a follow-up PR from this other one:
#74413

Nothing calls into these two methods, so we (@DavidSpickett,
@adrian-prantl, and I) agreed to remove them once we merged the previous
PR.
PortalPete added a commit to PortalPete/llvm-project that referenced this pull request Feb 7, 2024
…ment's value (llvm#74413)

It's more meaningful and actionable to indicate which element in the
array has an issue by returning that element's index instead of its
value. The value can be ambiguous if at least one other element has the
same value.

The first parameter for these methods is `idxs`, an array of indices
that represent a path from a (root) parent to on of its descendants,
typically though intermediate descendants. When the path leads to a
descendant that doesn't exist, the method is supposed to indicate where
things went wrong by setting an index to `&index_of_error`, the second
parameter.

The problem is the method sets `*index_of_error` to the index of the
most recent parent's child in the hierarchy, which isn't very useful if
there's more one index with the same value in the path.

In this example, each element in the path has a value that's the same as
another element.

```cpp
GetChildAtIndexPath({1, 2, 3, 3, 1, 1, 2}, &index_of_error);
```

Say the the second `1` in the path (the 5th element at `[4]`) doesn't
exist and the code returns a `nullptr`. In that situation, the code sets
`*index_of_error` to `1`, but that's an ambiguous hint can implicate the
1st, 5th, or 6th element (at `[0]`, `[4]`, or `[5]`).

It’s more helpful to set `*index_of_error` to `4` to clearly indicate
which element in `idxs` has the issue.
PortalPete added a commit to PortalPete/llvm-project that referenced this pull request Feb 7, 2024
…t.cpp (llvm#75870)

This a follow-up PR from this other one:
llvm#74413

Nothing calls into these two methods, so we (@DavidSpickett,
@adrian-prantl, and I) agreed to remove them once we merged the previous
PR.
qihangkong pushed a commit to rvgpu/rvgpu-llvm that referenced this pull request Apr 23, 2024
…t.cpp (#75870)

This a follow-up PR from this other one:
llvm/llvm-project#74413

Nothing calls into these two methods, so we (@DavidSpickett,
@adrian-prantl, and I) agreed to remove them once we merged the previous
PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants