From: Simon Marchi Date: Sun, 9 Feb 2025 05:51:00 +0000 (-0500) Subject: gdbsupport: add gdb::make_array_view overload to create from an array X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9044044c27c3619a18f58c8c235250264cd95a7e;p=thirdparty%2Fbinutils-gdb.git gdbsupport: add gdb::make_array_view overload to create from an array I think this overload will be useful for the following reasons. Consider a templated function like this: template void func(gdb::array_view view) {} Trying to pass an array to this function doesn't work, as template argument deduction fails: test.c:698:8: error: no matching function for call to ‘func(int [12])’ 698 | func (array); | ~~~~~^~~~~~~ test.c:686:6: note: candidate: ‘template void func(gdb::array_view)’ 686 | void func(gdb::array_view view) {} | ^~~~ test.c:686:6: note: template argument deduction/substitution failed: test.c:698:8: note: mismatched types ‘gdb::array_view’ and ‘int*’ 698 | func (array); | ~~~~~^~~~~~~ Similarly, trying to compare a view with an array doesn't work. This: int array[12]; gdb::array_view view; if (view == array) {} ... fails with: test.c:698:8: error: no matching function for call to ‘func(int [12])’ 698 | func (array); | ~~~~~^~~~~~~ test.c:686:6: note: candidate: ‘template void func(gdb::array_view)’ 686 | void func(gdb::array_view view) {} | ^~~~ test.c:686:6: note: template argument deduction/substitution failed: test.c:698:8: note: mismatched types ‘gdb::array_view’ and ‘int*’ 698 | func (array); | ~~~~~^~~~~~~ With this new overload, we can do: func (gdb::make_array_view (array)); and if (view == gdb::make_array_view (array)) {} This is not ideal, I wish that omitting `gdb::make_array_view` would just work, but at least it allows creating an array view and have the element type automatically deduced from the array type. If someone knows how to make these cases "just work", I would be happy to know how. Change-Id: I6a71919d2d5a385e6826801d53f5071b470fef5f Approved-By: Tom Tromey --- diff --git a/gdb/unittests/array-view-selftests.c b/gdb/unittests/array-view-selftests.c index c07b5720ef0..cc3fcfd8173 100644 --- a/gdb/unittests/array-view-selftests.c +++ b/gdb/unittests/array-view-selftests.c @@ -554,6 +554,19 @@ run_tests () SELF_CHECK (view[i] == data[i]); } + /* gdb::make_array_view with an array. */ + { + const gdb_byte data[] = {0x55, 0x66, 0x77, 0x88}; + const auto len = sizeof (data) / sizeof (data[0]); + const auto view = gdb::make_array_view (data); + + SELF_CHECK (view.data () == data); + SELF_CHECK (view.size () == len); + + for (std::size_t i = 0; i < len; ++i) + SELF_CHECK (view[i] == data[i]); + } + /* Test slicing. */ { gdb_byte data[] = {0x55, 0x66, 0x77, 0x88, 0x99}; diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h index 5054d7013fa..5dcf8d83990 100644 --- a/gdbsupport/array-view.h +++ b/gdbsupport/array-view.h @@ -294,6 +294,15 @@ make_array_view (U *array, size_t size) noexcept return {array, size}; } +/* Create an array view from an array. */ + +template +constexpr inline array_view +make_array_view (U (&array)[Size]) +{ + return {array}; +} + } /* namespace gdb */ #endif /* GDBSUPPORT_ARRAY_VIEW_H */