public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] 1/2 Make _GLIBCXX_DEBUG checks constexpr compatible
@ 2020-11-08 14:06 François Dumont
  2020-11-09 12:58 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: François Dumont @ 2020-11-08 14:06 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 3994 bytes --]

Now that __glibcxx_assert is constexpr compatible we can do the same for 
the _GLIBCXX_DEBUG equivalent.

I had also try to do the same on my own so this patch contains the 
string_view tests I had written when doing so.

I plan to activate some _GLIBCXX_DEBUG checks when _GLIBCXX_ASSERTIONS 
is defined but only the contant time checks. Is it ok to run checks like 
__check_partitioned_lower in constexpr ?

     libstdc++: Make _GLIBCXX_DEBUG checks constexpr compatible

     libstdc++-v3/ChangeLog:

             * include/debug/assertions.h 
(__glibcxx_requires_non_empty_range):
             Remove __builtin_expect.
             (__glibcxx_requires_subscript): Likewise.
             (__glibcxx_requires_nonempty): Likewise.
             * include/debug/formatter.h (__check_singular): Add C++11 
constexpr
             qualification.
             * include/debug/helper_functions.h (__check_singular): 
Likewise. Skip
             check if constant evaluated.
             (__valid_range): Do not skip check if constant evaluated.
             * include/debug/macros.h (_GLIBCXX_DEBUG_VERIFY_COND_AT): Add
             __builtin_expect.
             (_GLIBCXX_DEBUG_VERIFY_AT_F): Use __glibcxx_assert_1.
             * testsuite/21_strings/basic_string_view/element_access/char/
             back_constexpr_neg.cc: New test.
             * testsuite/21_strings/basic_string_view/element_access/char/
             constexpr.cc: New test.
             * testsuite/21_strings/basic_string_view/element_access/char/
             constexpr_neg.cc: New test.
             * testsuite/21_strings/basic_string_view/element_access/char/
             front_back_constexpr.cc: New test.
             * testsuite/21_strings/basic_string_view/element_access/char/
             front_constexpr_neg.cc: New test.
             * 
testsuite/21_strings/basic_string_view/element_access/wchar_t/
             back_constexpr_neg.cc: New test.
             * 
testsuite/21_strings/basic_string_view/element_access/wchar_t/
             constexpr.cc: New test.
             * 
testsuite/21_strings/basic_string_view/element_access/wchar_t/
             constexpr_neg.cc: New test.
             * 
testsuite/21_strings/basic_string_view/element_access/wchar_t/
             front_constexpr_neg.cc: New test.
             * testsuite/25_algorithms/lower_bound/debug/
             constexpr_partitioned_neg.cc: New test.
             * testsuite/25_algorithms/lower_bound/debug/
             constexpr_partitioned_pred_neg.cc: New test.
             * testsuite/25_algorithms/lower_bound/debug/
             constexpr_valid_range_neg.cc: New test.
             * testsuite/25_algorithms/lower_bound/debug/partitioned_neg.cc:
             New test.
             * 
testsuite/25_algorithms/lower_bound/debug/partitioned_pred_neg.cc:
             New test.
             * testsuite/25_algorithms/upper_bound/debug/
             constexpr_partitioned_neg.cc: New test.
             * testsuite/25_algorithms/upper_bound/debug/
             constexpr_partitioned_pred_neg.cc: New test.
             * testsuite/25_algorithms/upper_bound/debug/
             constexpr_valid_range_neg.cc: New test.
             * testsuite/25_algorithms/upper_bound/debug/partitioned_neg.cc:
             New test.
             * 
testsuite/25_algorithms/upper_bound/debug/partitioned_pred_neg.cc:
             New test.

Tested under Linux x86_64.

Ok to commit ?

François


[-- Attachment #2: debug_constexpr.patch --]
[-- Type: text/x-patch, Size: 35048 bytes --]

diff --git a/libstdc++-v3/include/debug/assertions.h b/libstdc++-v3/include/debug/assertions.h
index fb52e441424..16cf6bbe7ac 100644
--- a/libstdc++-v3/include/debug/assertions.h
+++ b/libstdc++-v3/include/debug/assertions.h
@@ -45,12 +45,12 @@
 
 // Verify that [_First, _Last) forms a non-empty iterator range.
 # define __glibcxx_requires_non_empty_range(_First,_Last)	\
-  __glibcxx_assert(__builtin_expect(_First != _Last, true))
+  __glibcxx_assert(_First != _Last)
 # define __glibcxx_requires_subscript(_N)	\
-  __glibcxx_assert(__builtin_expect(_N < this->size(), true))
+  __glibcxx_assert(_N < this->size())
 // Verify that the container is nonempty
 # define __glibcxx_requires_nonempty()		\
-  __glibcxx_assert(__builtin_expect(!this->empty(), true))
+  __glibcxx_assert(!this->empty())
 #endif
 
 #ifdef _GLIBCXX_DEBUG
diff --git a/libstdc++-v3/include/debug/formatter.h b/libstdc++-v3/include/debug/formatter.h
index c4283fe6047..8734ed06cb5 100644
--- a/libstdc++-v3/include/debug/formatter.h
+++ b/libstdc++-v3/include/debug/formatter.h
@@ -72,7 +72,8 @@ namespace __gnu_debug
   using std::type_info;
 
   template<typename _Iterator>
-    bool __check_singular(const _Iterator&);
+    _GLIBCXX_CONSTEXPR
+    bool __check_singular(_Iterator const&);
 
   class _Safe_sequence_base;
 
diff --git a/libstdc++-v3/include/debug/helper_functions.h b/libstdc++-v3/include/debug/helper_functions.h
index 62d5309257f..281e9c7390e 100644
--- a/libstdc++-v3/include/debug/helper_functions.h
+++ b/libstdc++-v3/include/debug/helper_functions.h
@@ -120,15 +120,29 @@ namespace __gnu_debug
   // We may have an iterator that derives from _Safe_iterator_base but isn't
   // a _Safe_iterator.
   template<typename _Iterator>
+    _GLIBCXX_CONSTEXPR
     inline bool
     __check_singular(_Iterator const& __x)
-    { return __check_singular_aux(std::__addressof(__x)); }
+    {
+      return
+#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
+	__builtin_is_constant_evaluated() ? false :
+#endif
+	__check_singular_aux(std::__addressof(__x));
+    }
 
   /** Non-NULL pointers are nonsingular. */
   template<typename _Tp>
+    _GLIBCXX_CONSTEXPR
     inline bool
     __check_singular(_Tp* const& __ptr)
-    { return __ptr == 0; }
+    {
+      return
+#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
+	__builtin_is_constant_evaluated() ? false :
+#endif
+	__ptr == 0;
+    }
 
   /** We say that integral types for a valid range, and defer to other
    *  routines to realize what to do with integral types instead of
@@ -225,11 +239,6 @@ namespace __gnu_debug
     __valid_range(_InputIterator __first, _InputIterator __last,
 		  typename _Distance_traits<_InputIterator>::__type& __dist)
     {
-#ifdef __cpp_lib_is_constant_evaluated
-      if (std::is_constant_evaluated())
-	// Detected by the compiler directly.
-	return true;
-#endif
       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
       return __valid_range_aux(__first, __last, __dist, _Integral());
     }
@@ -253,11 +262,6 @@ namespace __gnu_debug
     inline bool
     __valid_range(_InputIterator __first, _InputIterator __last)
     {
-#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
-      if (__builtin_is_constant_evaluated())
-	// Detected by the compiler directly.
-	return true;
-#endif
       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
       return __valid_range_aux(__first, __last, _Integral());
     }
diff --git a/libstdc++-v3/include/debug/macros.h b/libstdc++-v3/include/debug/macros.h
index ed606809a4d..ef4c76c747a 100644
--- a/libstdc++-v3/include/debug/macros.h
+++ b/libstdc++-v3/include/debug/macros.h
@@ -38,25 +38,15 @@
  * the user error and where the error is reported.
  *
  */
-#if 0 /* defined _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED */
-# define _GLIBCXX_DEBUG_VERIFY_COND_AT(_Cond,_ErrMsg,_File,_Line,_Func)	\
-  if (__builtin_is_constant_evaluated())				\
-    /* FIXME: Compilation error here when !_Cond. */			\
-    break;								\
-  if (! (_Cond))							\
+#define _GLIBCXX_DEBUG_VERIFY_COND_AT(_Cond,_ErrMsg,_File,_Line,_Func)	\
+  if (__builtin_expect(!bool(_Cond), false))				\
     __gnu_debug::_Error_formatter::_S_at(_File, _Line, _Func)		\
       ._ErrMsg._M_error()
-#else
-# define _GLIBCXX_DEBUG_VERIFY_COND_AT(_Cond,_ErrMsg,_File,_Line,_Func)	\
-  if (! (_Cond))							\
-    __gnu_debug::_Error_formatter::_S_at(_File, _Line, _Func)		\
-      ._ErrMsg._M_error()
-#endif
 
 #define _GLIBCXX_DEBUG_VERIFY_AT_F(_Cond,_ErrMsg,_File,_Line,_Func)	\
-  do									\
-  {									\
-    _GLIBCXX_DEBUG_VERIFY_COND_AT(_Cond,_ErrMsg,_File,_Line,_Func);	\
+  do {									\
+    __glibcxx_assert_1(_Cond)						\
+    { _GLIBCXX_DEBUG_VERIFY_COND_AT(_Cond,_ErrMsg,_File,_Line,_Func); } \
   } while (false)
 
 #define _GLIBCXX_DEBUG_VERIFY_AT(_Cond,_ErrMsg,_File,_Line)		\
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc
new file mode 100644
index 00000000000..d5049ff246c
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++17 -D_GLIBCXX_ASSERTIONS" }
+// { dg-do compile { target c++17 xfail *-*-* } }
+
+#include <string_view>
+
+typedef std::string_view string_view_type;
+
+constexpr char
+back()
+{
+  string_view_type s("");
+  return s.back();
+}
+
+static_assert(back() != 'a'); // { dg-error "non-constant condition" }
+
+// { dg-prune-output "in 'constexpr' expansion" }
+// { dg-prune-output "failed_assertion" }
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr.cc
new file mode 100644
index 00000000000..aa2a08ebe83
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr.cc
@@ -0,0 +1,31 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++17 } }
+
+#include <string_view>
+
+constexpr char
+test()
+{
+  typedef std::string_view string_view_type;
+  string_view_type s("abcd");
+  return s[0];
+}
+
+static_assert(test() == 'a');
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc
new file mode 100644
index 00000000000..a302556866c
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc
@@ -0,0 +1,34 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++17 -D_GLIBCXX_ASSERTIONS" }
+// { dg-do compile { target c++17 xfail *-*-* } }
+
+#include <string_view>
+
+constexpr char
+test()
+{
+  typedef std::string_view string_view_type;
+  string_view_type s("abcd");
+  return s[s.length()];
+}
+
+static_assert(test() == 0); // { dg-error "non-constant condition" }
+
+// { dg-prune-output "in 'constexpr' expansion" }
+// { dg-prune-output "failed_assertion" }
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_back_constexpr.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_back_constexpr.cc
new file mode 100644
index 00000000000..7ead45ddcf6
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_back_constexpr.cc
@@ -0,0 +1,41 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++17 } }
+
+#include <string_view>
+
+typedef std::string_view string_view_type;
+
+constexpr char
+front()
+{
+  string_view_type s("abcd");
+  return s.front();
+}
+
+static_assert(front() == 'a');
+
+constexpr char
+back()
+{
+  string_view_type s("abcd");
+  return s.back();
+}
+
+static_assert(back() == 'd');
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc
new file mode 100644
index 00000000000..8fad31a07dd
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++17 -D_GLIBCXX_ASSERTIONS" }
+// { dg-do compile { target c++17 xfail *-*-* } }
+
+#include <string_view>
+
+typedef std::string_view string_view_type;
+
+constexpr char
+front()
+{
+  string_view_type s("");
+  return s.front();
+}
+
+static_assert(front() != 'a'); // { dg-error "non-constant condition" }
+
+// { dg-prune-output "in 'constexpr' expansion" }
+// { dg-prune-output "failed_assertion" }
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc
new file mode 100644
index 00000000000..0500a2de600
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++17 -D_GLIBCXX_ASSERTIONS" }
+// { dg-do compile { target c++17 xfail *-*-* } }
+
+#include <string_view>
+
+typedef std::wstring_view string_view_type;
+
+constexpr wchar_t
+back()
+{
+  string_view_type s(L"");
+  return s.back();
+}
+
+static_assert(back() != L'a'); // { dg-error "non-constant condition" }
+
+// { dg-prune-output "in 'constexpr' expansion" }
+// { dg-prune-output "failed_assertion" }
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr.cc
new file mode 100644
index 00000000000..f4171628e9f
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr.cc
@@ -0,0 +1,31 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++17 } }
+
+#include <string_view>
+
+constexpr wchar_t
+test()
+{
+  typedef std::wstring_view string_view_type;
+  string_view_type s(L"abcd");
+  return s[0];
+}
+
+static_assert(test() == L'a');
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc
new file mode 100644
index 00000000000..299a73b8c8e
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc
@@ -0,0 +1,34 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++17 -D_GLIBCXX_ASSERTIONS" }
+// { dg-do compile { target c++17 xfail *-*-* } }
+
+#include <string_view>
+
+constexpr wchar_t
+test()
+{
+  typedef std::wstring_view string_view_type;
+  string_view_type s(L"abcd");
+  return s[4];
+}
+
+static_assert(test() == 0); // { dg-error "non-constant condition" }
+
+// { dg-prune-output "in 'constexpr' expansion" }
+// { dg-prune-output "failed_assertion" }
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc
new file mode 100644
index 00000000000..009b4e41a73
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++17 -D_GLIBCXX_ASSERTIONS" }
+// { dg-do compile { target c++17 xfail *-*-* } }
+
+#include <string_view>
+
+typedef std::wstring_view string_view_type;
+
+constexpr wchar_t
+front()
+{
+  string_view_type s(L"");
+  return s.front();
+}
+
+static_assert(front() != L'a'); // { dg-error "non-constant condition" }
+
+// { dg-prune-output "in 'constexpr' expansion" }
+// { dg-prune-output "failed_assertion" }
diff --git a/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/constexpr_partitioned_neg.cc b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/constexpr_partitioned_neg.cc
new file mode 100644
index 00000000000..ee4b19e9e7b
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/constexpr_partitioned_neg.cc
@@ -0,0 +1,48 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a xfail *-*-* } }
+// { dg-require-debug-mode { } }
+
+#include <algorithm>
+#include <array>
+
+struct A
+{
+  int _i;
+
+  constexpr bool
+  operator<(const A& a) const
+  { return _i < a._i; }
+};
+
+constexpr bool
+test()
+{
+  constexpr std::array<A, 12> ca0{{0, 1, 2, 3, 4, 5, 6, 5, 8, 9, 10, 11}};
+
+  constexpr A a6{ 6 };
+  const auto it = std::lower_bound(ca0.begin(), ca0.end(), a6);
+
+  return true;
+}
+
+static_assert(test()); // { dg-error "" }
+
+// { dg-prune-output "failed_assertion" }
+// { dg-prune-output "in 'constexpr'" }
diff --git a/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/constexpr_partitioned_pred_neg.cc b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/constexpr_partitioned_pred_neg.cc
new file mode 100644
index 00000000000..01eb38a8807
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/constexpr_partitioned_pred_neg.cc
@@ -0,0 +1,38 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a xfail *-*-* } }
+// { dg-require-debug-mode { } }
+
+#include <algorithm>
+#include <array>
+
+constexpr bool
+test()
+{
+  constexpr std::array<int, 12> ca0{{0, 1, 2, 3, 4, 5, 6, 5, 8, 9, 10, 11}};
+
+  const auto it = std::lower_bound(ca0.begin(), ca0.end(), 6, std::less<int>());
+
+  return true;
+}
+
+static_assert(test()); // { dg-error "" }
+
+// { dg-prune-output "failed_assertion" }
+// { dg-prune-output "in 'constexpr'" }
diff --git a/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/constexpr_valid_range_neg.cc b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/constexpr_valid_range_neg.cc
new file mode 100644
index 00000000000..787352aa94b
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/constexpr_valid_range_neg.cc
@@ -0,0 +1,52 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a xfail *-*-* } }
+// { dg-require-debug-mode { } }
+
+#include <algorithm>
+#include <array>
+
+constexpr bool
+test1()
+{
+  constexpr std::array<int, 12> ca0{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
+
+  const auto outbb = std::lower_bound(ca0.end(), ca0.begin(), 6);
+
+  return true;
+}
+
+static_assert(test1()); // { dg-error "" }
+
+constexpr bool
+test2()
+{
+  constexpr std::array<int, 12> ca0{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
+
+  const auto outcc = std::lower_bound(ca0.end(), ca0.begin(), 6,
+				      std::less<int>());
+
+  return true;
+}
+
+static_assert(test2()); // { dg-error "" }
+
+// { dg-prune-output "failed_assertion" }
+// { dg-prune-output "in 'constexpr'" }
+
diff --git a/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_neg.cc b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_neg.cc
new file mode 100644
index 00000000000..cdf4a3a2ed4
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_neg.cc
@@ -0,0 +1,46 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-do run { xfail *-*-* } }
+// { dg-require-debug-mode "" }
+
+#include <algorithm>
+
+struct A
+{
+  A(int i) : _i(i)
+  { }
+
+  int _i;
+
+  bool
+  operator<(const A& a) const
+  { return _i < a._i; }
+};
+
+void test01()
+{
+  A as[] = { 0, 1, 2, 0, 2, 3 };
+  std::lower_bound(as, as + 6, A(1));
+}
+
+int
+main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_pred_neg.cc b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_pred_neg.cc
new file mode 100644
index 00000000000..9eda3046745
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_pred_neg.cc
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-do run { xfail *-*-* } }
+// { dg-require-debug-mode "" }
+
+#include <algorithm>
+
+void test01()
+{
+  int as[] = { 0, 1, 0, 2, 3 };
+  std::lower_bound(as, as + 5, 1, std::less<int>());
+}
+
+
+int
+main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/constexpr_partitioned_neg.cc b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/constexpr_partitioned_neg.cc
new file mode 100644
index 00000000000..716cdfa85ae
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/constexpr_partitioned_neg.cc
@@ -0,0 +1,48 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a xfail *-*-* } }
+// { dg-require-debug-mode { } }
+
+#include <algorithm>
+#include <array>
+
+struct A
+{
+  int _i;
+
+  constexpr bool
+  operator<(const A& a) const
+  { return _i == a._i; }
+};
+
+constexpr bool
+test()
+{
+  constexpr std::array<A, 12> ca0{{0, 1, 2, 3, 4, 5, 6, 5, 8, 9, 10, 11}};
+
+  constexpr A a6{ 6 };
+  const auto it = std::upper_bound(ca0.begin(), ca0.end(), a6);
+
+  return true;
+}
+
+static_assert(test()); // { dg-error "" }
+
+// { dg-prune-output "failed_assertion" }
+// { dg-prune-output "in 'constexpr'" }
diff --git a/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/constexpr_partitioned_pred_neg.cc b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/constexpr_partitioned_pred_neg.cc
new file mode 100644
index 00000000000..2307e17bfa5
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/constexpr_partitioned_pred_neg.cc
@@ -0,0 +1,38 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a xfail *-*-* } }
+// { dg-require-debug-mode { } }
+
+#include <algorithm>
+#include <array>
+
+constexpr bool
+test()
+{
+  constexpr std::array<int, 12> ca0{{0, 1, 2, 3, 4, 5, 7, 6, 8, 9, 10, 11}};
+
+  const auto it = std::upper_bound(ca0.begin(), ca0.end(), 6, std::less<int>());
+
+  return true;
+}
+
+static_assert(test()); // { dg-error "" }
+
+// { dg-prune-output "failed_assertion" }
+// { dg-prune-output "in 'constexpr'" }
diff --git a/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/constexpr_valid_range_neg.cc b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/constexpr_valid_range_neg.cc
new file mode 100644
index 00000000000..bcce2c48a1f
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/constexpr_valid_range_neg.cc
@@ -0,0 +1,51 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a xfail *-*-* } }
+// { dg-require-debug-mode { } }
+
+#include <algorithm>
+#include <array>
+
+constexpr bool
+test1()
+{
+  constexpr std::array<int, 12> ca0{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
+
+  const auto outbb = std::upper_bound(ca0.end(), ca0.begin(), 6);
+
+  return true;
+}
+
+static_assert(test1()); // { dg-error "" }
+
+constexpr bool
+test2()
+{
+  constexpr std::array<int, 12> ca0{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
+
+  const auto outcc = std::upper_bound(ca0.end(), ca0.begin(), 6,
+				      std::less<int>());
+
+  return true;
+}
+
+static_assert(test2()); // { dg-error "" }
+
+// { dg-prune-output "failed_assertion" }
+// { dg-prune-output "in 'constexpr'" }
diff --git a/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_neg.cc b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_neg.cc
new file mode 100644
index 00000000000..cbae81a08f3
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_neg.cc
@@ -0,0 +1,46 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-do run { xfail *-*-* } }
+// { dg-require-debug-mode "" }
+
+#include <algorithm>
+
+struct A
+{
+  A(int i) : _i(i)
+  { }
+
+  int _i;
+
+  bool
+  operator<(const A& a) const
+  { return _i < a._i; }
+};
+
+void test01()
+{
+  A as[] = { 0, 2, 1, 3, 4, 5 };
+  std::upper_bound(as, as + 6, A(1));
+}
+
+int
+main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_pred_neg.cc b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_pred_neg.cc
new file mode 100644
index 00000000000..5a42c2b32c0
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_pred_neg.cc
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-do run { xfail *-*-* } }
+// { dg-require-debug-mode "" }
+
+#include <algorithm>
+
+void test01()
+{
+  int as[] = { 0, 2, 1, 3, 4 };
+  std::upper_bound(as, as + 5, 1, std::less<int>());
+}
+
+
+int
+main()
+{
+  test01();
+  return 0;
+}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] 1/2 Make _GLIBCXX_DEBUG checks constexpr compatible
  2020-11-08 14:06 [PATCH] 1/2 Make _GLIBCXX_DEBUG checks constexpr compatible François Dumont
@ 2020-11-09 12:58 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2020-11-09 12:58 UTC (permalink / raw)
  To: François Dumont; +Cc: libstdc++, gcc-patches

On 08/11/20 15:06 +0100, François Dumont via Libstdc++ wrote:
>Now that __glibcxx_assert is constexpr compatible we can do the same 
>for the _GLIBCXX_DEBUG equivalent.
>
>I had also try to do the same on my own so this patch contains the 
>string_view tests I had written when doing so.
>
>I plan to activate some _GLIBCXX_DEBUG checks when _GLIBCXX_ASSERTIONS 
>is defined but only the contant time checks. Is it ok to run checks 
>like __check_partitioned_lower in constexpr ?

Hmm, I don't *think* it's possible to detect the additional calls to
the comparison function during constant evaluation. So I think the
only concern is the extra work the compiler has to do, i.e. the extra
time it takes to compile.

The constant-time checks should be OK though.


>    libstdc++: Make _GLIBCXX_DEBUG checks constexpr compatible
[snip]
>Ok to commit ?

Yes, thanks.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-09 12:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-08 14:06 [PATCH] 1/2 Make _GLIBCXX_DEBUG checks constexpr compatible François Dumont
2020-11-09 12:58 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).