Hi
Following PR 78420 patch I realized that we can use similar
technique to have assertions in string_view implementations.
I also rename testsuite files expected to XFAIL as they should have
a trailing '_neg'. It fixes 4 XPASS when run in debug mode
Note that I also try to use a static_assert when
__builtin_constant_v return true but it is not constant enough. Could it
be a gcc issue ?
Tested under Linux x86_64 normal and debug modes.
Ok to commit ? Now ?
François
diff --git a/libstdc++-v3/include/experimental/string_view b/libstdc++-v3/include/experimental/string_view
index e42d5ac..97f9312 100644
--- a/libstdc++-v3/include/experimental/string_view
+++ b/libstdc++-v3/include/experimental/string_view
@@ -178,8 +178,9 @@ inline namespace fundamentals_v1
constexpr const _CharT&
operator[](size_type __pos) const
{
- // TODO: Assert to restore in a way compatible with the constexpr.
- // __glibcxx_assert(__pos < this->_M_len);
+ if (!__builtin_constant_p(__pos))
+ __glibcxx_assert(__pos < this->_M_len);
+
return *(this->_M_str + __pos);
}
@@ -198,16 +199,18 @@ inline namespace fundamentals_v1
constexpr const _CharT&
front() const
{
- // TODO: Assert to restore in a way compatible with the constexpr.
- // __glibcxx_assert(this->_M_len > 0);
+ if (!__builtin_constant_p(this->_M_len))
+ __glibcxx_assert(this->_M_len > 0);
+
return *this->_M_str;
}
constexpr const _CharT&
back() const
{
- // TODO: Assert to restore in a way compatible with the constexpr.
- // __glibcxx_assert(this->_M_len > 0);
+ if (!__builtin_constant_p(this->_M_len))
+ __glibcxx_assert(this->_M_len > 0);
+
return *(this->_M_str + this->_M_len - 1);
}
diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view
index 9f39df8..22b168b 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -169,8 +169,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr const _CharT&
operator[](size_type __pos) const noexcept
{
- // TODO: Assert to restore in a way compatible with the constexpr.
- // __glibcxx_assert(__pos < this->_M_len);
+ if (!__builtin_constant_p(__pos))
+ __glibcxx_assert(__pos < this->_M_len);
+
return *(this->_M_str + __pos);
}
@@ -187,16 +188,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr const _CharT&
front() const noexcept
{
- // TODO: Assert to restore in a way compatible with the constexpr.
- // __glibcxx_assert(this->_M_len > 0);
+ if (!__builtin_constant_p(this->_M_len))
+ __glibcxx_assert(this->_M_len > 0);
+
return *this->_M_str;
}
constexpr const _CharT&
back() const noexcept
{
- // TODO: Assert to restore in a way compatible with the constexpr.
- // __glibcxx_assert(this->_M_len > 0);
+ if (!__builtin_constant_p(this->_M_len))
+ __glibcxx_assert(this->_M_len > 0);
+
return *(this->_M_str + this->_M_len - 1);
}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/2.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/2.cc
deleted file mode 100644
index 4ee2b64..0000000
--- a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/2.cc
+++ /dev/null
@@ -1,30 +0,0 @@
-// { dg-do run { xfail *-*-* } }
-// { dg-options "-std=gnu++17 -O0" }
-// { dg-require-debug-mode "" }
-
-// Copyright (C) 2013-2018 Free Software Foundation, Inc.
-//
-// This file is part of the GNU ISO C++ Library. This library is free
-// software; you can redistribute it and/or modify it under the
-// terms of the GNU General Public License as published by the
-// Free Software Foundation; either version 3, or (at your option)
-// any later version.
-
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License along
-// with this library; see the file COPYING3. If not see
-// <http://www.gnu.org/licenses/>.
-
-#include <string_view>
-
-int
-main()
-{
- typedef std::string_view string_view_type;
- string_view_type s;
- s[0]; // abort
-}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/2_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/2_neg.cc
new file mode 100644
index 0000000..4ee2b64
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/2_neg.cc
@@ -0,0 +1,30 @@
+// { dg-do run { xfail *-*-* } }
+// { dg-options "-std=gnu++17 -O0" }
+// { dg-require-debug-mode "" }
+
+// Copyright (C) 2013-2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <string_view>
+
+int
+main()
+{
+ typedef std::string_view string_view_type;
+ string_view_type s;
+ s[0]; // abort
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_neg.cc
new file mode 100644
index 0000000..12afb7e
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_neg.cc
@@ -0,0 +1,37 @@
+// { dg-do run { xfail *-*-* } }
+// { dg-options "-std=gnu++17 -O0" }
+// { dg-require-debug-mode "" }
+
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <string_view>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ std::string_view str;
+ str.back(); // abort
+}
+
+int
+main()
+{
+ test01();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_neg.cc
new file mode 100644
index 0000000..3db7f05
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_neg.cc
@@ -0,0 +1,37 @@
+// { dg-do run { xfail *-*-* } }
+// { dg-options "-std=gnu++17 -O0" }
+// { dg-require-debug-mode "" }
+
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <string_view>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ std::string_view str;
+ str.front(); // abort
+}
+
+int
+main()
+{
+ test01();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/2.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/2.cc
deleted file mode 100644
index 5b7421f..0000000
--- a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/2.cc
+++ /dev/null
@@ -1,32 +0,0 @@
-// { dg-do run { xfail *-*-* } }
-// { dg-options "-std=gnu++17 -O0" }
-// { dg-require-debug-mode "" }
-
-// Copyright (C) 2013-2018 Free Software Foundation, Inc.
-//
-// This file is part of the GNU ISO C++ Library. This library is free
-// software; you can redistribute it and/or modify it under the
-// terms of the GNU General Public License as published by the
-// Free Software Foundation; either version 3, or (at your option)
-// any later version.
-
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License along
-// with this library; see the file COPYING3. If not see
-// <http://www.gnu.org/licenses/>.
-
-#include <string_view>
-
-// libstdc++/21674
-// NB: Should work without any inlining or optimizations (ie. -O0).
-int
-main()
-{
- typedef std::wstring_view string_view_type;
- string_view_type s;
- s[0]; // abort
-}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/2_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/2_neg.cc
new file mode 100644
index 0000000..5b7421f
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/2_neg.cc
@@ -0,0 +1,32 @@
+// { dg-do run { xfail *-*-* } }
+// { dg-options "-std=gnu++17 -O0" }
+// { dg-require-debug-mode "" }
+
+// Copyright (C) 2013-2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <string_view>
+
+// libstdc++/21674
+// NB: Should work without any inlining or optimizations (ie. -O0).
+int
+main()
+{
+ typedef std::wstring_view string_view_type;
+ string_view_type s;
+ s[0]; // abort
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_neg.cc
new file mode 100644
index 0000000..5620ed3
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_neg.cc
@@ -0,0 +1,37 @@
+// { dg-do run { xfail *-*-* } }
+// { dg-options "-std=gnu++17 -O0" }
+// { dg-require-debug-mode "" }
+
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <string_view>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ std::wstring_view str;
+ str.back(); // abort
+}
+
+int
+main()
+{
+ test01();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_neg.cc
new file mode 100644
index 0000000..6a63143
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_neg.cc
@@ -0,0 +1,37 @@
+// { dg-do run { xfail *-*-* } }
+// { dg-options "-std=gnu++17 -O0" }
+// { dg-require-debug-mode "" }
+
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <string_view>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ std::wstring_view str;
+ str.front(); // abort
+}
+
+int
+main()
+{
+ test01();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/experimental/string_view/element_access/char/2.cc b/libstdc++-v3/testsuite/experimental/string_view/element_access/char/2.cc
deleted file mode 100644
index bd566e6..0000000
--- a/libstdc++-v3/testsuite/experimental/string_view/element_access/char/2.cc
+++ /dev/null
@@ -1,30 +0,0 @@
-// { dg-do run { target c++14 xfail *-*-* } }
-// { dg-options "-O0" }
-// { dg-require-debug-mode "" }
-
-// Copyright (C) 2013-2018 Free Software Foundation, Inc.
-//
-// This file is part of the GNU ISO C++ Library. This library is free
-// software; you can redistribute it and/or modify it under the
-// terms of the GNU General Public License as published by the
-// Free Software Foundation; either version 3, or (at your option)
-// any later version.
-
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License along
-// with this library; see the file COPYING3. If not see
-// <http://www.gnu.org/licenses/>.
-
-#include <experimental/string_view>
-
-int
-main()
-{
- typedef std::experimental::string_view string_view_type;
- string_view_type s;
- s[0]; // abort
-}
diff --git a/libstdc++-v3/testsuite/experimental/string_view/element_access/char/2_neg.cc b/libstdc++-v3/testsuite/experimental/string_view/element_access/char/2_neg.cc
new file mode 100644
index 0000000..bd566e6
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/string_view/element_access/char/2_neg.cc
@@ -0,0 +1,30 @@
+// { dg-do run { target c++14 xfail *-*-* } }
+// { dg-options "-O0" }
+// { dg-require-debug-mode "" }
+
+// Copyright (C) 2013-2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <experimental/string_view>
+
+int
+main()
+{
+ typedef std::experimental::string_view string_view_type;
+ string_view_type s;
+ s[0]; // abort
+}
diff --git a/libstdc++-v3/testsuite/experimental/string_view/element_access/wchar_t/2.cc b/libstdc++-v3/testsuite/experimental/string_view/element_access/wchar_t/2.cc
deleted file mode 100644
index 6c97c81..0000000
--- a/libstdc++-v3/testsuite/experimental/string_view/element_access/wchar_t/2.cc
+++ /dev/null
@@ -1,32 +0,0 @@
-// { dg-do run { target c++14 xfail *-*-* } }
-// { dg-options "-O0" }
-// { dg-require-debug-mode "" }
-
-// Copyright (C) 2013-2018 Free Software Foundation, Inc.
-//
-// This file is part of the GNU ISO C++ Library. This library is free
-// software; you can redistribute it and/or modify it under the
-// terms of the GNU General Public License as published by the
-// Free Software Foundation; either version 3, or (at your option)
-// any later version.
-
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License along
-// with this library; see the file COPYING3. If not see
-// <http://www.gnu.org/licenses/>.
-
-#include <experimental/string_view>
-
-// libstdc++/21674
-// NB: Should work without any inlining or optimizations (ie. -O0).
-int
-main()
-{
- typedef std::experimental::wstring_view string_view_type;
- string_view_type s;
- s[0]; // abort
-}
diff --git a/libstdc++-v3/testsuite/experimental/string_view/element_access/wchar_t/2_neg.cc b/libstdc++-v3/testsuite/experimental/string_view/element_access/wchar_t/2_neg.cc
new file mode 100644
index 0000000..6c97c81
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/string_view/element_access/wchar_t/2_neg.cc
@@ -0,0 +1,32 @@
+// { dg-do run { target c++14 xfail *-*-* } }
+// { dg-options "-O0" }
+// { dg-require-debug-mode "" }
+
+// Copyright (C) 2013-2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <experimental/string_view>
+
+// libstdc++/21674
+// NB: Should work without any inlining or optimizations (ie. -O0).
+int
+main()
+{
+ typedef std::experimental::wstring_view string_view_type;
+ string_view_type s;
+ s[0]; // abort
+}