[libc++] Introduce __debug_db_insert_c()

There are a lot of
```
#if _LIBCPP_DEBUG_LEVEL == 2
    __get_db()->__insert_c(this);
#endif
```

This patch introduces `__debug_db_insert_c()` to put the `#if` in one central place.

Reviewed By: ldionne, #libc

Spies: libcxx-commits

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

NOKEYCHECK=True
GitOrigin-RevId: e3cf70502cae1b29ece72a0d6ef28352ca2319fa
diff --git a/include/string b/include/string
index 1b803bb..04d3e41 100644
--- a/include/string
+++ b/include/string
@@ -829,10 +829,7 @@
     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
-        if (!__libcpp_is_constant_evaluated())
-            __get_db()->__insert_c(this);
-#endif
+      _VSTD::__debug_db_insert_c(this);
     }
 
     template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
@@ -1841,10 +1838,7 @@
     _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
      : __r_(__default_init_tag(), __default_init_tag())
 {
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
     __zero();
 }
 
@@ -1858,10 +1852,7 @@
 #endif
 : __r_(__default_init_tag(), __a)
 {
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
     __zero();
 }
 
@@ -1921,10 +1912,7 @@
 {
     _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
     __init(__s, traits_type::length(__s));
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -1932,12 +1920,9 @@
 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);
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
+    __init(__s, __n);
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -1947,10 +1932,7 @@
 {
     _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
     __init(__s, __n);
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -1962,11 +1944,7 @@
     else
         __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
                                   __str.__get_long_size());
-
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -1979,10 +1957,7 @@
     else
         __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
                                   __str.__get_long_size());
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2017,12 +1992,10 @@
     : __r_(_VSTD::move(__str.__r_))
 {
     __str.__zero();
+    _VSTD::__debug_db_insert_c(this);
 #if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated()) {
-        __get_db()->__insert_c(this);
-        if (__is_long())
-            __get_db()->swap(this, &__str);
-    }
+    if (!__libcpp_is_constant_evaluated() && __is_long())
+        __get_db()->swap(this, &__str);
 #endif
 }
 
@@ -2038,12 +2011,10 @@
         __r_.first().__r = __str.__r_.first().__r;
         __str.__zero();
     }
+    _VSTD::__debug_db_insert_c(this);
 #if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated()) {
-        __get_db()->__insert_c(this);
-        if (__is_long())
-            __get_db()->swap(this, &__str);
-    }
+    if (!__libcpp_is_constant_evaluated() && __is_long())
+        __get_db()->swap(this, &__str);
 #endif
 }
 
@@ -2079,10 +2050,7 @@
      : __r_(__default_init_tag(), __default_init_tag())
 {
     __init(__n, __c);
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2091,10 +2059,7 @@
     : __r_(__default_init_tag(), __a)
 {
     __init(__n, __c);
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2107,10 +2072,7 @@
     if (__pos > __str_sz)
         this->__throw_out_of_range();
     __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2123,10 +2085,7 @@
     if (__pos > __str_sz)
         this->__throw_out_of_range();
     __init(__str.data() + __pos, __str_sz - __pos);
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2138,10 +2097,7 @@
     __self_view __sv0 = __t;
     __self_view __sv = __sv0.substr(__pos, __n);
     __init(__sv.data(), __sv.size());
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2151,10 +2107,7 @@
 {
     __self_view __sv = __t;
     __init(__sv.data(), __sv.size());
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2164,10 +2117,7 @@
 {
     __self_view __sv = __t;
     __init(__sv.data(), __sv.size());
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2247,10 +2197,7 @@
      : __r_(__default_init_tag(), __default_init_tag())
 {
     __init(__first, __last);
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2261,10 +2208,7 @@
     : __r_(__default_init_tag(), __a)
 {
     __init(__first, __last);
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 #ifndef _LIBCPP_CXX03_LANG
@@ -2276,10 +2220,7 @@
      : __r_(__default_init_tag(), __default_init_tag())
 {
     __init(__il.begin(), __il.end());
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -2290,10 +2231,7 @@
     : __r_(__default_init_tag(), __a)
 {
     __init(__il.begin(), __il.end());
-#if _LIBCPP_DEBUG_LEVEL == 2
-    if (!__libcpp_is_constant_evaluated())
-        __get_db()->__insert_c(this);
-#endif
+    _VSTD::__debug_db_insert_c(this);
 }
 
 #endif // _LIBCPP_CXX03_LANG