-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
[lldb] Return index of element in ValueObject path instead of the element's value #74413
Conversation
@llvm/pr-subscribers-lldb Author: Pete Lawrence (PortalPete) Changesrdar://119169160 Full diff: https://github.com/llvm/llvm-project/pull/74413.diff 1 Files Affected:
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;
}
|
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. 🙃
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Should we change the documentation of these functions? |
return root; | ||
} | ||
|
||
current_index += 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto for current_index++
There was a problem hiding this comment.
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.
...
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
26891e6
to
5d99172
Compare
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. |
…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
5d99172
to
5731517
Compare
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. |
…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.
…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.
…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.
…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.
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.
Say the the second
1
in the path (the 5th element at[4]
) doesn't exist and the code returns anullptr
. In that situation, the code sets*index_of_error
to1
, 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
to4
to clearly indicate which element inidxs
has the issue.