Add gdb::string_view

We had a few times the need for a data structure that does essentially
what C++17's std::string_view does, which is to give an std::string-like
interface (only the read-only operations) to an arbitrary character
buffer.

This patch adapts the files copied from libstdc++ by the previous patch
to integrate them with GDB.  Here's a summary of the changes:

  * Remove things related to wstring_view, u16string_view and
  u32string_view (I don't think we need them, but we can always add them
  later).

  * Remove usages of _GLIBCXX_BEGIN_NAMESPACE_VERSION and
  _GLIBCXX_END_NAMESPACE_VERSION.

  * Put the code in the gdb namespace.  I had to add a few "std::" in
  front of std type usages.

  * Change __throw_out_of_range_fmt() for error().

  * Make gdb::string_view an alias of std::string_view when building
  with >= c++17.

  * Remove a bunch of constexpr, because they are not valid in c++11
  (e.g. they are not a single return line).

  * Use std::common_type<_Tp>::type instead of std::common_type_t<_Tp>,
  because c++11 doesn't have the later.

  * Remove the #pragma GCC system_header, since that silences some
  warnings that we might want to have if we're doing something not
  correctly.

  * Remove operator ""sv.  It would need a lot of work to make all
  supported compilers happy, and we can easily live without it.

  * Remove operator<<.  It is implemented using __ostream_insert (a
  libstdc++ internal).  Bringing it in might be possible, but I don't
  think that would be worth the effort, since we don't really use
  streams at the moment.

  * Replace internal libstdc++ asserts ( __glibcxx_assert and
  __glibcxx_requires_string_len) with gdb_assert.

  * Remove hash helpers, because they use libstdc++ internal functions.
  If we need them we always import them later.

The string_view class in cli/cli-script.c is removed and its usage
replaced with the new gdb::string_view.

gdb/ChangeLog:

	* common/gdb_string_view.h: Remove libstdc++ implementation
	details, adjust to gdb reality.
	* common/gdb_string_view.tcc: Likewise.
	* cli/cli-script.c (struct string_view): Remove.
	(user_args) <m_args>: Change element type to gdb::string_view.
	(user_args::insert_args): Adjust.
This commit is contained in:
Simon Marchi
2018-04-09 13:31:04 -04:00
committed by Simon Marchi
parent 7adcdf08e7
commit 8345c4a267
4 changed files with 126 additions and 260 deletions

View File

@@ -1,5 +1,9 @@
// Components for manipulating non-owning sequences of characters -*- C++ -*-
// Note: This file has been stolen from the gcc repo
// (libstdc++-v3/include/experimental/bits/string_view.tcc) and has local
// modifications.
// Copyright (C) 2013-2018 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
@@ -31,27 +35,17 @@
// N3762 basic_string_view library
//
#ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC
#define _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC 1
#ifndef GDB_STRING_VIEW_TCC
#define GDB_STRING_VIEW_TCC 1
#pragma GCC system_header
#if __cplusplus >= 201402L
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
namespace experimental
{
inline namespace fundamentals_v1
namespace gdb
{
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
{
__glibcxx_requires_string_len(__str, __n);
gdb_assert (__str != nullptr || __n == 0);
if (__n == 0)
return __pos <= this->_M_len ? __pos : npos;
@@ -68,7 +62,7 @@ inline namespace fundamentals_v1
}
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
find(_CharT __c, size_type __pos) const noexcept
{
@@ -84,11 +78,11 @@ inline namespace fundamentals_v1
}
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept
{
__glibcxx_requires_string_len(__str, __n);
gdb_assert (__str != nullptr || __n == 0);
if (__n <= this->_M_len)
{
@@ -104,7 +98,7 @@ inline namespace fundamentals_v1
}
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
rfind(_CharT __c, size_type __pos) const noexcept
{
@@ -121,11 +115,11 @@ inline namespace fundamentals_v1
}
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
find_first_of(const _CharT* __str, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__str, __n);
gdb_assert (__str != nullptr || __n == 0);
for (; __n && __pos < this->_M_len; ++__pos)
{
const _CharT* __p = traits_type::find(__str, __n,
@@ -137,11 +131,11 @@ inline namespace fundamentals_v1
}
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
find_last_of(const _CharT* __str, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__str, __n);
gdb_assert (__str != nullptr || __n == 0);
size_type __size = this->size();
if (__size && __n)
{
@@ -158,11 +152,11 @@ inline namespace fundamentals_v1
}
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__str, __n);
gdb_assert (__str != nullptr || __n == 0);
for (; __pos < this->_M_len; ++__pos)
if (!traits_type::find(__str, __n, this->_M_str[__pos]))
return __pos;
@@ -170,7 +164,7 @@ inline namespace fundamentals_v1
}
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
find_first_not_of(_CharT __c, size_type __pos) const noexcept
{
@@ -181,11 +175,11 @@ inline namespace fundamentals_v1
}
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__str, __n);
gdb_assert (__str != nullptr || __n == 0);
size_type __size = this->_M_len;
if (__size)
{
@@ -202,7 +196,7 @@ inline namespace fundamentals_v1
}
template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type
/*constexpr*/ typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>::
find_last_not_of(_CharT __c, size_type __pos) const noexcept
{
@@ -220,12 +214,6 @@ inline namespace fundamentals_v1
}
return npos;
}
} // namespace fundamentals_v1
} // namespace experimental
} // namespace gdb
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // __cplusplus <= 201103L
#endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW_TCC
#endif // GDB_STRING_VIEW_TCC