[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-12-02 Thread Michael Buch via Phabricator via lldb-commits
Michael137 added a comment.

In D138558#3963732 , @labath wrote:

> You may want to check that this kind of automatic dereferencing does not send 
> lldb into a tailspin if the printed data structure is recursive. I know we 
> had problems like that with smart pointer pretty printers.
>
> I'd try some code like:
>
>   #include 
>   #include 
>   
>   struct A {
> std::ranges::ref_view> a;
>   };
>   
>   int main() {
>   std::vector v;
>   v.push_back(A{v});
>   v[0].a = v;
>   // print v ?
>   }

@labath good point

E.g., the following would cause such unbounded recursion (didn't find a good 
way of triggering it using the actual ref_view since it's not default 
constructible, but it's probably doable):

  #include 
  
  namespace std {
  inline namespace __1 {
  namespace ranges {
  template
  struct ref_view {
  T* __range_;
  };
  }
  }
  }
  
  struct Foo {
  std::ranges::ref_view> a;
  };
  
  int main() {
  Foo f;
  std::vector v;
  v.push_back(f);
  
  std::ranges::ref_view> r{.__range_ = };
  f.a = r;
  
  return 0;
  }


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138558/new/

https://reviews.llvm.org/D138558

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-12-01 Thread Pavel Labath via Phabricator via lldb-commits
labath added a comment.

You may want to check that this kind of automatic dereferencing does not send 
lldb into a tailspin if the printed data structure is recursive. I know we had 
problems like that with smart pointer pretty printers.

I'd try some code like:

  #include 
  #include 
  
  struct A {
std::ranges::ref_view> a;
  };
  
  int main() {
  std::vector v;
  v.push_back(A{v});
  v[0].a = v;
  // print v ?
  }


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138558/new/

https://reviews.llvm.org/D138558

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-11-30 Thread Adrian Prantl via Phabricator via lldb-commits
aprantl added inline comments.



Comment at: lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp:36
+  lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
+// Since we only have a single child, return it
+assert(idx == 0);

To be very nitpicking: LLVM style wants full sentences in comments, so let's 
add a `.` at the end. :-)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138558/new/

https://reviews.llvm.org/D138558

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-11-30 Thread Michael Buch via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG77b220524541: [lldb][DataFormatter] Add 
std::ranges::ref_view formatter (authored by Michael137).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138558/new/

https://reviews.llvm.org/D138558

Files:
  lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
  lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp

Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
@@ -0,0 +1,27 @@
+#include 
+#include 
+#include 
+#include 
+
+using string_vec = std::vector;
+
+string_vec svec{"First", "Second", "Third", "Fourth"};
+
+struct Foo {
+  string_vec vec = svec;
+};
+
+int main() {
+  {
+auto single = std::ranges::ref_view(svec[0]);
+auto all = std::views::all(svec);
+auto subset = all | std::views::take(2);
+std::puts("Break here");
+  }
+
+  {
+Foo f[2];
+auto view = std::ranges::ref_view(f);
+std::puts("Break here");
+  }
+}
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
@@ -0,0 +1,65 @@
+"""
+Test LLDB's std::ranges::ref_view formatter
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class LibcxxRangesRefViewDataFormatterTestCase(TestBase):
+
+def check_string_vec_children(self):
+return [ValueCheck(name='[0]', summary='"First"'),
+ValueCheck(name='[1]', summary='"Second"'),
+ValueCheck(name='[2]', summary='"Third"'),
+ValueCheck(name='[3]', summary='"Fourth"')]
+
+def check_string_vec_ref_view(self):
+return ValueCheck(
+name='*__range_',
+summary='size=4',
+children=self.check_string_vec_children())
+
+def check_foo(self):
+return ValueCheck(
+name='vec',
+children=self.check_string_vec_children())
+
+@add_test_categories(["libc++"])
+def test_with_run_command(self):
+"""Test that std::ranges::ref_view is formatted correctly when printed.
+"""
+self.build()
+(self.target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
+self, 'Break here', lldb.SBFileSpec('main.cpp', False))
+
+# Check ref_view over a std::string
+self.expect_var_path('single',
+ children=[ValueCheck(
+   name='*__range_',
+   summary='"First"')])
+
+# Check all_view, which is a ref_view in this case
+self.expect_var_path('all',
+ children=[self.check_string_vec_ref_view()])
+
+# Check take_view format. Embeds a ref_view
+self.expect_var_path('subset',
+ children=[
+ ValueCheck(children=[self.check_string_vec_ref_view()]),
+ ValueCheck(name='__count_', value='2')
+ ])
+
+lldbutil.continue_to_breakpoint(self.process(), bkpt)
+
+# Check ref_view over custom type 'struct Foo'
+self.expect_var_path('view',
+ children=[ValueCheck(
+name='*__range_',
+children=[
+ValueCheck(name='[0]', type='Foo', children=[self.check_foo()]),
+ValueCheck(name='[1]', type='Foo', children=[self.check_foo()])
+])
+   ])
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
@@ -0,0 +1,6 @@
+USE_LIBCPP := 

[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-11-30 Thread Michael Buch via Phabricator via lldb-commits
Michael137 marked an inline comment as done.
Michael137 added inline comments.



Comment at: lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp:41
+private:
+  lldb::ValueObjectSP m_range_sp = nullptr; ///< Pointer to the dereferenced
+///< __range_ member

aprantl wrote:
> Personally I prefer 
> ```
> ///Pointer to the dereferenced __range_ member.
> lldb::ValueObjectSP m_range_sp = nullptr;
> ```
> for longer comments.
Done



Comment at: lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp:55
+  return 1;
+}
+

aprantl wrote:
> I wonder if it would be more readable to move those one-line function 
> definitions into the declaration?
Looks better, done


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138558/new/

https://reviews.llvm.org/D138558

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-11-30 Thread Michael Buch via Phabricator via lldb-commits
Michael137 updated this revision to Diff 478929.
Michael137 added a comment.

- Move short definitions inline
- Fix doxygen comment


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138558/new/

https://reviews.llvm.org/D138558

Files:
  lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
  lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp

Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
@@ -0,0 +1,27 @@
+#include 
+#include 
+#include 
+#include 
+
+using string_vec = std::vector;
+
+string_vec svec{"First", "Second", "Third", "Fourth"};
+
+struct Foo {
+  string_vec vec = svec;
+};
+
+int main() {
+  {
+auto single = std::ranges::ref_view(svec[0]);
+auto all = std::views::all(svec);
+auto subset = all | std::views::take(2);
+std::puts("Break here");
+  }
+
+  {
+Foo f[2];
+auto view = std::ranges::ref_view(f);
+std::puts("Break here");
+  }
+}
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
@@ -0,0 +1,65 @@
+"""
+Test LLDB's std::ranges::ref_view formatter
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class LibcxxRangesRefViewDataFormatterTestCase(TestBase):
+
+def check_string_vec_children(self):
+return [ValueCheck(name='[0]', summary='"First"'),
+ValueCheck(name='[1]', summary='"Second"'),
+ValueCheck(name='[2]', summary='"Third"'),
+ValueCheck(name='[3]', summary='"Fourth"')]
+
+def check_string_vec_ref_view(self):
+return ValueCheck(
+name='*__range_',
+summary='size=4',
+children=self.check_string_vec_children())
+
+def check_foo(self):
+return ValueCheck(
+name='vec',
+children=self.check_string_vec_children())
+
+@add_test_categories(["libc++"])
+def test_with_run_command(self):
+"""Test that std::ranges::ref_view is formatted correctly when printed.
+"""
+self.build()
+(self.target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
+self, 'Break here', lldb.SBFileSpec('main.cpp', False))
+
+# Check ref_view over a std::string
+self.expect_var_path('single',
+ children=[ValueCheck(
+   name='*__range_',
+   summary='"First"')])
+
+# Check all_view, which is a ref_view in this case
+self.expect_var_path('all',
+ children=[self.check_string_vec_ref_view()])
+
+# Check take_view format. Embeds a ref_view
+self.expect_var_path('subset',
+ children=[
+ ValueCheck(children=[self.check_string_vec_ref_view()]),
+ ValueCheck(name='__count_', value='2')
+ ])
+
+lldbutil.continue_to_breakpoint(self.process(), bkpt)
+
+# Check ref_view over custom type 'struct Foo'
+self.expect_var_path('view',
+ children=[ValueCheck(
+name='*__range_',
+children=[
+ValueCheck(name='[0]', type='Foo', children=[self.check_foo()]),
+ValueCheck(name='[1]', type='Foo', children=[self.check_foo()])
+])
+   ])
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
@@ -0,0 +1,6 @@
+USE_LIBCPP := 1
+
+CXX_SOURCES := main.cpp
+CXXFLAGS_EXTRAS := 

[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-11-29 Thread Adrian Prantl via Phabricator via lldb-commits
aprantl accepted this revision.
aprantl added a comment.
This revision is now accepted and ready to land.

Small nitpicks, otherwise good!




Comment at: lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp:41
+private:
+  lldb::ValueObjectSP m_range_sp = nullptr; ///< Pointer to the dereferenced
+///< __range_ member

Personally I prefer 
```
///Pointer to the dereferenced __range_ member.
lldb::ValueObjectSP m_range_sp = nullptr;
```
for longer comments.



Comment at: lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp:55
+  return 1;
+}
+

I wonder if it would be more readable to move those one-line function 
definitions into the declaration?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138558/new/

https://reviews.llvm.org/D138558

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-11-23 Thread Michael Buch via Phabricator via lldb-commits
Michael137 updated this revision to Diff 477440.
Michael137 added a comment.

- Clean up test


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138558/new/

https://reviews.llvm.org/D138558

Files:
  lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
  lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp

Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
@@ -0,0 +1,27 @@
+#include 
+#include 
+#include 
+#include 
+
+using string_vec = std::vector;
+
+string_vec svec{"First", "Second", "Third", "Fourth"};
+
+struct Foo {
+  string_vec vec = svec;
+};
+
+int main() {
+  {
+auto single = std::ranges::ref_view(svec[0]);
+auto all = std::views::all(svec);
+auto subset = all | std::views::take(2);
+std::puts("Break here");
+  }
+
+  {
+Foo f[2];
+auto view = std::ranges::ref_view(f);
+std::puts("Break here");
+  }
+}
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
@@ -0,0 +1,65 @@
+"""
+Test LLDB's std::ranges::ref_view formatter
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class LibcxxRangesRefViewDataFormatterTestCase(TestBase):
+
+def check_string_vec_children(self):
+return [ValueCheck(name='[0]', summary='"First"'),
+ValueCheck(name='[1]', summary='"Second"'),
+ValueCheck(name='[2]', summary='"Third"'),
+ValueCheck(name='[3]', summary='"Fourth"')]
+
+def check_string_vec_ref_view(self):
+return ValueCheck(
+name='*__range_',
+summary='size=4',
+children=self.check_string_vec_children())
+
+def check_foo(self):
+return ValueCheck(
+name='vec',
+children=self.check_string_vec_children())
+
+@add_test_categories(["libc++"])
+def test_with_run_command(self):
+"""Test that std::ranges::ref_view is formatted correctly when printed.
+"""
+self.build()
+(self.target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
+self, 'Break here', lldb.SBFileSpec('main.cpp', False))
+
+# Check ref_view over a std::string
+self.expect_var_path('single',
+ children=[ValueCheck(
+   name='*__range_',
+   summary='"First"')])
+
+# Check all_view, which is a ref_view in this case
+self.expect_var_path('all',
+ children=[self.check_string_vec_ref_view()])
+
+# Check take_view format. Embeds a ref_view
+self.expect_var_path('subset',
+ children=[
+ ValueCheck(children=[self.check_string_vec_ref_view()]),
+ ValueCheck(name='__count_', value='2')
+ ])
+
+lldbutil.continue_to_breakpoint(self.process(), bkpt)
+
+# Check ref_view over custom type 'struct Foo'
+self.expect_var_path('view',
+ children=[ValueCheck(
+name='*__range_',
+children=[
+ValueCheck(name='[0]', type='Foo', children=[self.check_foo()]),
+ValueCheck(name='[1]', type='Foo', children=[self.check_foo()])
+])
+   ])
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
@@ -0,0 +1,6 @@
+USE_LIBCPP := 1
+
+CXX_SOURCES := main.cpp
+CXXFLAGS_EXTRAS := -std=c++20
+
+include Makefile.rules
Index: 

[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-11-23 Thread Michael Buch via Phabricator via lldb-commits
Michael137 updated this revision to Diff 477439.
Michael137 added a comment.

- Use typedef in test


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138558/new/

https://reviews.llvm.org/D138558

Files:
  lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
  lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp

Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
@@ -0,0 +1,27 @@
+#include 
+#include 
+#include 
+#include 
+
+using string_vec = std::vector;
+
+string_vec svec{"First", "Second", "Third", "Fourth"};
+
+struct Foo {
+  string_vec vec = svec;
+};
+
+int main() {
+  {
+auto single = std::ranges::ref_view(svec[0]);
+auto all = std::views::all(svec);
+auto subset = all | std::views::take(2);
+std::puts("Break here");
+  }
+
+  {
+Foo f[2];
+auto view = std::ranges::ref_view(f);
+std::puts("Break here");
+  }
+}
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
@@ -0,0 +1,65 @@
+"""
+Test LLDB's std::ranges::ref_view formatter
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class LibcxxRangesRefViewDataFormatterTestCase(TestBase):
+
+def check_string_vec_children(self):
+return [ValueCheck(name='[0]', summary='"First"'),
+ValueCheck(name='[1]', summary='"Second"'),
+ValueCheck(name='[2]', summary='"Third"'),
+ValueCheck(name='[3]', summary='"Fourth"')]
+
+def check_string_vec_ref_view(self):
+return ValueCheck(
+name='*__range_',
+summary='size=4',
+children=self.check_string_vec_children())
+
+def check_foo(self):
+return ValueCheck(
+name="vec",
+children=self.check_string_vec_children())
+
+@add_test_categories(["libc++"])
+def test_with_run_command(self):
+"""Test that std::ranges::ref_view is formatted correctly when printed.
+"""
+self.build()
+(self.target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
+self, 'Break here', lldb.SBFileSpec('main.cpp', False))
+
+# Check ref_view over a std::string
+self.expect_var_path('single',
+ children=[ValueCheck(
+   name='*__range_',
+   summary='"First"')])
+
+# Check all_view, which is a ref_view in this case
+self.expect_var_path('all',
+ children=[self.check_string_vec_ref_view()])
+
+# Check take_view format. Embeds a ref_view
+self.expect_var_path('subset',
+ children=[
+ ValueCheck(children=[self.check_string_vec_ref_view()]),
+ ValueCheck(name='__count_', value='2')
+ ])
+
+lldbutil.continue_to_breakpoint(self.process(), bkpt)
+
+# Check ref_view over custom type 'struct Foo'
+self.expect_var_path('view',
+ children=[ValueCheck(
+name='*__range_',
+children=[
+ValueCheck(name='[0]', type="Foo", children=[self.check_foo()]),
+ValueCheck(name='[1]', type="Foo", children=[self.check_foo()])
+])
+   ])
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
@@ -0,0 +1,6 @@
+USE_LIBCPP := 1
+
+CXX_SOURCES := main.cpp
+CXXFLAGS_EXTRAS := -std=c++20
+
+include Makefile.rules

[Lldb-commits] [PATCH] D138558: [lldb][DataFormatter] Add std::ranges::ref_view formatter

2022-11-23 Thread Michael Buch via Phabricator via lldb-commits
Michael137 created this revision.
Herald added a project: All.
Michael137 requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

This patch adds a formatter for `std::ranges::ref_view`.
It simply holds a `T*`, so all this formatter does is dereference
this pointer and format it as `T` would be.

**Testing**

- Added API tests


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138558

Files:
  lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
  lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
  
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp

Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/main.cpp
@@ -0,0 +1,27 @@
+#include 
+#include 
+#include 
+#include 
+
+using string_vec = std::vector;
+
+string_vec svec{"First", "Second", "Third", "Fourth"};
+
+struct Foo {
+  std::vector vec = svec;
+};
+
+int main() {
+  {
+auto single = std::ranges::ref_view(svec[0]);
+auto all = std::views::all(svec);
+auto subset = all | std::views::take(2);
+std::puts("Break here");
+  }
+
+  {
+Foo f[2];
+auto view = std::ranges::ref_view(f);
+std::puts("Break here");
+  }
+}
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
===
--- /dev/null
+++ lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/TestDataFormatterLibcxxRangesRefView.py
@@ -0,0 +1,65 @@
+"""
+Test LLDB's std::ranges::ref_view formatter
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class LibcxxRangesRefViewDataFormatterTestCase(TestBase):
+
+def check_string_vec_children(self):
+return [ValueCheck(name='[0]', summary='"First"'),
+ValueCheck(name='[1]', summary='"Second"'),
+ValueCheck(name='[2]', summary='"Third"'),
+ValueCheck(name='[3]', summary='"Fourth"')]
+
+def check_string_vec_ref_view(self):
+return ValueCheck(
+name='*__range_',
+summary='size=4',
+children=self.check_string_vec_children())
+
+def check_foo(self):
+return ValueCheck(
+name="vec",
+children=self.check_string_vec_children())
+
+@add_test_categories(["libc++"])
+def test_with_run_command(self):
+"""Test that std::ranges::ref_view is formatted correctly when printed.
+"""
+self.build()
+(self.target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
+self, 'Break here', lldb.SBFileSpec('main.cpp', False))
+
+# Check ref_view over a std::string
+self.expect_var_path('single',
+ children=[ValueCheck(
+   name='*__range_',
+   summary='"First"')])
+
+# Check all_view, which is a ref_view in this case
+self.expect_var_path('all',
+ children=[self.check_string_vec_ref_view()])
+
+# Check take_view format. Embeds a ref_view
+self.expect_var_path('subset',
+ children=[
+ ValueCheck(children=[self.check_string_vec_ref_view()]),
+ ValueCheck(name='__count_', value='2')
+ ])
+
+lldbutil.continue_to_breakpoint(self.process(), bkpt)
+
+# Check ref_view over custom type 'struct Foo'
+self.expect_var_path('view',
+ children=[ValueCheck(
+name='*__range_',
+children=[
+ValueCheck(name='[0]', type="Foo", children=[self.check_foo()]),
+ValueCheck(name='[1]', type="Foo", children=[self.check_foo()])
+])
+   ])
Index: lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/ranges/ref_view/Makefile
===
--- /dev/null
+++