-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[lldb][libc++] Adds valarray data formatters. #80609
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
mordante
merged 1 commit into
llvm:main
from
mordante:reviews/lldb/valarray_data_formatter
Feb 10, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
lldb/source/Plugins/Language/CPlusPlus/LibCxxValarray.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//===-- LibCxxValarray.cpp ------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "LibCxx.h" | ||
|
||
#include "lldb/Core/ValueObject.h" | ||
#include "lldb/DataFormatters/FormattersHelpers.h" | ||
#include <optional> | ||
|
||
using namespace lldb; | ||
using namespace lldb_private; | ||
using namespace lldb_private::formatters; | ||
|
||
namespace lldb_private { | ||
namespace formatters { | ||
class LibcxxStdValarraySyntheticFrontEnd : public SyntheticChildrenFrontEnd { | ||
public: | ||
LibcxxStdValarraySyntheticFrontEnd(lldb::ValueObjectSP valobj_sp); | ||
|
||
~LibcxxStdValarraySyntheticFrontEnd() override; | ||
|
||
size_t CalculateNumChildren() override; | ||
|
||
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override; | ||
|
||
lldb::ChildCacheState Update() override; | ||
|
||
bool MightHaveChildren() override; | ||
|
||
size_t GetIndexOfChildWithName(ConstString name) override; | ||
|
||
private: | ||
/// A non-owning pointer to valarray's __begin_ member. | ||
ValueObject *m_start = nullptr; | ||
/// A non-owning pointer to valarray's __end_ member. | ||
ValueObject *m_finish = nullptr; | ||
/// The type of valarray's template argument T. | ||
CompilerType m_element_type; | ||
/// The sizeof valarray's template argument T. | ||
uint32_t m_element_size = 0; | ||
}; | ||
|
||
} // namespace formatters | ||
} // namespace lldb_private | ||
|
||
lldb_private::formatters::LibcxxStdValarraySyntheticFrontEnd:: | ||
LibcxxStdValarraySyntheticFrontEnd(lldb::ValueObjectSP valobj_sp) | ||
: SyntheticChildrenFrontEnd(*valobj_sp), m_element_type() { | ||
if (valobj_sp) | ||
Update(); | ||
} | ||
|
||
lldb_private::formatters::LibcxxStdValarraySyntheticFrontEnd:: | ||
~LibcxxStdValarraySyntheticFrontEnd() { | ||
// these need to stay around because they are child objects who will follow | ||
// their parent's life cycle | ||
// delete m_start; | ||
// delete m_finish; | ||
} | ||
|
||
size_t lldb_private::formatters::LibcxxStdValarraySyntheticFrontEnd:: | ||
CalculateNumChildren() { | ||
if (!m_start || !m_finish) | ||
return 0; | ||
uint64_t start_val = m_start->GetValueAsUnsigned(0); | ||
uint64_t finish_val = m_finish->GetValueAsUnsigned(0); | ||
|
||
if (start_val == 0 || finish_val == 0) | ||
return 0; | ||
|
||
if (start_val >= finish_val) | ||
return 0; | ||
|
||
size_t num_children = (finish_val - start_val); | ||
if (num_children % m_element_size) | ||
return 0; | ||
return num_children / m_element_size; | ||
} | ||
|
||
lldb::ValueObjectSP | ||
lldb_private::formatters::LibcxxStdValarraySyntheticFrontEnd::GetChildAtIndex( | ||
size_t idx) { | ||
if (!m_start || !m_finish) | ||
return lldb::ValueObjectSP(); | ||
|
||
uint64_t offset = idx * m_element_size; | ||
offset = offset + m_start->GetValueAsUnsigned(0); | ||
StreamString name; | ||
name.Printf("[%" PRIu64 "]", (uint64_t)idx); | ||
return CreateValueObjectFromAddress(name.GetString(), offset, | ||
m_backend.GetExecutionContextRef(), | ||
m_element_type); | ||
} | ||
|
||
lldb::ChildCacheState | ||
lldb_private::formatters::LibcxxStdValarraySyntheticFrontEnd::Update() { | ||
m_start = m_finish = nullptr; | ||
|
||
CompilerType type = m_backend.GetCompilerType(); | ||
if (type.GetNumTemplateArguments() == 0) | ||
return ChildCacheState::eRefetch; | ||
|
||
m_element_type = type.GetTypeTemplateArgument(0); | ||
if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) | ||
m_element_size = *size; | ||
|
||
if (m_element_size == 0) | ||
return ChildCacheState::eRefetch; | ||
|
||
ValueObjectSP start = m_backend.GetChildMemberWithName("__begin_"); | ||
ValueObjectSP finish = m_backend.GetChildMemberWithName("__end_"); | ||
|
||
if (!start || !finish) | ||
return ChildCacheState::eRefetch; | ||
|
||
m_start = start.get(); | ||
m_finish = finish.get(); | ||
|
||
return ChildCacheState::eRefetch; | ||
} | ||
|
||
bool lldb_private::formatters::LibcxxStdValarraySyntheticFrontEnd:: | ||
MightHaveChildren() { | ||
return true; | ||
} | ||
|
||
size_t lldb_private::formatters::LibcxxStdValarraySyntheticFrontEnd:: | ||
GetIndexOfChildWithName(ConstString name) { | ||
if (!m_start || !m_finish) | ||
return std::numeric_limits<size_t>::max(); | ||
return ExtractIndexFromString(name.GetCString()); | ||
} | ||
|
||
lldb_private::SyntheticChildrenFrontEnd * | ||
lldb_private::formatters::LibcxxStdValarraySyntheticFrontEndCreator( | ||
CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) { | ||
if (!valobj_sp) | ||
return nullptr; | ||
return new LibcxxStdValarraySyntheticFrontEnd(valobj_sp); | ||
} |
5 changes: 5 additions & 0 deletions
5
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/valarray/Makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CXX_SOURCES := main.cpp | ||
|
||
USE_LIBCPP := 1 | ||
|
||
include Makefile.rules |
78 changes: 78 additions & 0 deletions
78
...ties/data-formatter/data-formatter-stl/libcxx/valarray/TestDataFormatterLibcxxValarray.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
Test lldb data formatter subsystem. | ||
""" | ||
|
||
|
||
import lldb | ||
from lldbsuite.test.decorators import * | ||
from lldbsuite.test.lldbtest import * | ||
from lldbsuite.test import lldbutil | ||
|
||
|
||
class LibcxxChronoDataFormatterTestCase(TestBase): | ||
@add_test_categories(["libc++"]) | ||
def test_with_run_command(self): | ||
"""Test that that file and class static variables display correctly.""" | ||
self.build() | ||
(self.target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint( | ||
self, "break here", lldb.SBFileSpec("main.cpp", False) | ||
) | ||
|
||
self.expect( | ||
"frame variable va_int", | ||
substrs=[ | ||
"va_int = size=4", | ||
"[0] = 0", | ||
"[1] = 0", | ||
"[2] = 0", | ||
"[3] = 0", | ||
"}", | ||
], | ||
) | ||
|
||
lldbutil.continue_to_breakpoint(process, bkpt) | ||
self.expect( | ||
"frame variable va_int", | ||
substrs=[ | ||
"va_int = size=4", | ||
"[0] = 1", | ||
"[1] = 12", | ||
"[2] = 123", | ||
"[3] = 1234", | ||
"}", | ||
], | ||
) | ||
|
||
# check access-by-index | ||
self.expect("frame variable va_int[0]", substrs=["1"]) | ||
self.expect("frame variable va_int[1]", substrs=["12"]) | ||
self.expect("frame variable va_int[2]", substrs=["123"]) | ||
self.expect("frame variable va_int[3]", substrs=["1234"]) | ||
self.expect( | ||
"frame variable va_int[4]", | ||
error=True, | ||
substrs=['array index 4 is not valid for "(valarray<int>) va_int"'], | ||
) | ||
|
||
self.expect( | ||
"frame variable va_double", | ||
substrs=[ | ||
"va_double = size=4", | ||
"[0] = 1", | ||
"[1] = 0.5", | ||
"[2] = 0.25", | ||
"[3] = 0.125", | ||
"}", | ||
], | ||
) | ||
|
||
# check access-by-index | ||
self.expect("frame variable va_double[0]", substrs=["1"]) | ||
self.expect("frame variable va_double[1]", substrs=["0.5"]) | ||
self.expect("frame variable va_double[2]", substrs=["0.25"]) | ||
self.expect("frame variable va_double[3]", substrs=["0.125"]) | ||
self.expect( | ||
"frame variable va_double[4]", | ||
error=True, | ||
substrs=['array index 4 is not valid for "(valarray<double>) va_double"'], | ||
) |
17 changes: 17 additions & 0 deletions
17
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/valarray/main.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
#include <valarray> | ||
|
||
int main() { | ||
|
||
std::valarray<int> va_int(4); | ||
std::cout << "break here"; | ||
|
||
va_int[0] = 1; | ||
va_int[1] = 12; | ||
va_int[2] = 123; | ||
va_int[3] = 1234; | ||
|
||
std::valarray<double> va_double({1.0, 0.5, 0.25, 0.125}); | ||
|
||
std::cout << "break here\n"; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.