[libcxx-commits] [libcxx] 5675b61 - [libc++] Disable _LIBCPP_DEBUG_ASSERT during constant evaluation

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 17 02:17:01 PST 2021


Author: Nikolas Klauser
Date: 2021-12-17T11:15:53+01:00
New Revision: 5675b6112aa9d0dddfa0e4f326ffa58e30c94c81

URL: https://github.com/llvm/llvm-project/commit/5675b6112aa9d0dddfa0e4f326ffa58e30c94c81
DIFF: https://github.com/llvm/llvm-project/commit/5675b6112aa9d0dddfa0e4f326ffa58e30c94c81.diff

LOG: [libc++] Disable _LIBCPP_DEBUG_ASSERT during constant evaluation

Disable `_LIBCPP_DEBUG_ASSERT` and debug iterators in <string> during constant evaluation

Reviewed By: ldionne, #libc

Spies: goncharov, libcxx-commits

Differential Revision: https://reviews.llvm.org/D115788

Added: 
    

Modified: 
    libcxx/include/__debug
    libcxx/include/bit
    libcxx/include/string

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__debug b/libcxx/include/__debug
index e25039c088c6b..42f6cef4c07fa 100644
--- a/libcxx/include/__debug
+++ b/libcxx/include/__debug
@@ -34,7 +34,7 @@
 #   define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)
 #   define _LIBCPP_ASSERT_IMPL(x, m) ((x) ? (void)0 : _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
 #elif _LIBCPP_DEBUG_LEVEL == 2
-#   define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(x, m)
+#   define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(__libcpp_is_constant_evaluated() || (x), m)
 #   define _LIBCPP_ASSERT_IMPL(x, m) ((x) ? (void)0 : _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
 #else
 #   error _LIBCPP_DEBUG_LEVEL must be one of 0, 1, 2

diff  --git a/libcxx/include/bit b/libcxx/include/bit
index 0aab83e7a6ebe..57a13768c493f 100644
--- a/libcxx/include/bit
+++ b/libcxx/include/bit
@@ -308,7 +308,7 @@ bit_ceil(_Tp __t) noexcept
 {
     if (__t < 2) return 1;
     const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
-    _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
+    _LIBCPP_ASSERT(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
 
     if constexpr (sizeof(_Tp) >= sizeof(unsigned))
         return _Tp{1} << __n;

diff  --git a/libcxx/include/string b/libcxx/include/string
index 63699b06be94b..9049d7acbb9bb 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -826,9 +826,10 @@ public:
     basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
       _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
       __init(__s, traits_type::length(__s));
-#   if _LIBCPP_DEBUG_LEVEL == 2
-      __get_db()->__insert_c(this);
-#   endif
+#if _LIBCPP_DEBUG_LEVEL == 2
+        if (!__libcpp_is_constant_evaluated())
+            __get_db()->__insert_c(this);
+#endif
     }
 
     template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
@@ -1785,7 +1786,8 @@ void
 basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
 {
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__invalidate_all(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__invalidate_all(this);
 #endif
 }
 
@@ -1795,22 +1797,24 @@ void
 basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
 {
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __c_node* __c = __get_db()->__find_c_and_lock(this);
-    if (__c)
-    {
-        const_pointer __new_last = __get_pointer() + __pos;
-        for (__i_node** __p = __c->end_; __p != __c->beg_; )
+    if (!__libcpp_is_constant_evaluated()) {
+        __c_node* __c = __get_db()->__find_c_and_lock(this);
+        if (__c)
         {
-            --__p;
-            const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
-            if (__i->base() > __new_last)
+            const_pointer __new_last = __get_pointer() + __pos;
+            for (__i_node** __p = __c->end_; __p != __c->beg_; )
             {
-                (*__p)->__c_ = nullptr;
-                if (--__c->end_ != __p)
-                    _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
+                --__p;
+                const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
+                if (__i->base() > __new_last)
+                {
+                    (*__p)->__c_ = nullptr;
+                    if (--__c->end_ != __p)
+                        _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
+                }
             }
+            __get_db()->unlock();
         }
-        __get_db()->unlock();
     }
 #else
     (void)__pos;
@@ -1824,7 +1828,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string()
      : __r_(__default_init_tag(), __default_init_tag())
 {
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
     __zero();
 }
@@ -1840,7 +1845,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __
 : __r_(__default_init_tag(), __a)
 {
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
     __zero();
 }
@@ -1902,7 +1908,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const
     _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
     __init(__s, traits_type::length(__s));
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -1911,10 +1918,11 @@ inline
 basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
      : __r_(__default_init_tag(), __default_init_tag())
 {
-    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
-    __init(__s, __n);
+      _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
+      __init(__s, __n);
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -1926,7 +1934,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_
     _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
     __init(__s, __n);
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -1941,7 +1950,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __st
                                   __str.__get_long_size());
 
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -1956,7 +1966,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(
         __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
                                   __str.__get_long_size());
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -1993,9 +2004,11 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
 {
     __str.__zero();
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
-    if (__is_long())
-        __get_db()->swap(this, &__str);
+    if (!__libcpp_is_constant_evaluated()) {
+        __get_db()->__insert_c(this);
+        if (__is_long())
+            __get_db()->swap(this, &__str);
+    }
 #endif
 }
 
@@ -2012,9 +2025,11 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, co
         __str.__zero();
     }
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
-    if (__is_long())
-        __get_db()->swap(this, &__str);
+    if (!__libcpp_is_constant_evaluated()) {
+        __get_db()->__insert_c(this);
+        if (__is_long())
+            __get_db()->swap(this, &__str);
+    }
 #endif
 }
 
@@ -2051,7 +2066,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __
 {
     __init(__n, __c);
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2062,7 +2078,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __
 {
     __init(__n, __c);
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2077,7 +2094,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __st
         this->__throw_out_of_range();
     __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2092,7 +2110,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __st
         this->__throw_out_of_range();
     __init(__str.data() + __pos, __str_sz - __pos);
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2106,7 +2125,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(
     __self_view __sv = __sv0.substr(__pos, __n);
     __init(__sv.data(), __sv.size());
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2118,7 +2138,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
     __self_view __sv = __t;
     __init(__sv.data(), __sv.size());
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2130,7 +2151,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _
     __self_view __sv = __t;
     __init(__sv.data(), __sv.size());
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2212,7 +2234,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first,
 {
     __init(__first, __last);
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2225,7 +2248,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first,
 {
     __init(__first, __last);
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2239,7 +2263,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(
 {
     __init(__il.begin(), __il.end());
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2252,7 +2277,8 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(
 {
     __init(__il.begin(), __il.end());
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__insert_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__insert_c(this);
 #endif
 }
 
@@ -2262,7 +2288,8 @@ template <class _CharT, class _Traits, class _Allocator>
 basic_string<_CharT, _Traits, _Allocator>::~basic_string()
 {
 #if _LIBCPP_DEBUG_LEVEL == 2
-    __get_db()->__erase_c(this);
+    if (!__libcpp_is_constant_evaluated())
+        __get_db()->__erase_c(this);
 #endif
     if (__is_long())
         __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
@@ -3467,11 +3494,13 @@ basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
 #endif
 {
 #if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__is_long())
-        __get_db()->__invalidate_all(this);
-    if (!__str.__is_long())
-        __get_db()->__invalidate_all(&__str);
-    __get_db()->swap(this, &__str);
+    if (!__libcpp_is_constant_evaluated()) {
+        if (!__is_long())
+            __get_db()->__invalidate_all(this);
+        if (!__str.__is_long())
+            __get_db()->__invalidate_all(&__str);
+        __get_db()->swap(this, &__str);
+    }
 #endif
     _LIBCPP_ASSERT(
         __alloc_traits::propagate_on_container_swap::value ||


        


More information about the libcxx-commits mailing list