[libcxx-commits] [libcxx] dfb20d4 - [libc++][format] Improve ABI stability.

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 23 08:37:25 PST 2021


Author: Mark de Wever
Date: 2021-12-23T17:37:12+01:00
New Revision: dfb20d4d19ec80e2e3e0985e08642567019b2068

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

LOG: [libc++][format] Improve ABI stability.

During the review of D115991 @vitaut pointed out the enum shouldn't
depend on whether or not _LIBCPP_HAS_NO_INT128 is defined. The current
implementation lets the enum's ABI depend on this configuration option
without a good cause.

Reviewed By: ldionne, #libc

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

Added: 
    libcxx/test/libcxx/utilities/format/format.arguments/format.arg/arg_t.compile.pass.cpp

Modified: 
    libcxx/include/__format/format_arg.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__format/format_arg.h b/libcxx/include/__format/format_arg.h
index a9a8c1f0da031..59429c13d4154 100644
--- a/libcxx/include/__format/format_arg.h
+++ b/libcxx/include/__format/format_arg.h
@@ -37,21 +37,20 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
 
 namespace __format {
-/** The type stored in @ref basic_format_arg. */
+/// The type stored in @ref basic_format_arg.
+///
+/// @note The 128-bit types are unconditionally in the list to avoid the values
+/// of the enums to depend on the availability of 128-bit integers.
 enum class _LIBCPP_ENUM_VIS __arg_t : uint8_t {
   __none,
   __boolean,
   __char_type,
   __int,
   __long_long,
-#ifndef _LIBCPP_HAS_NO_INT128
   __i128,
-#endif
   __unsigned,
   __unsigned_long_long,
-#ifndef _LIBCPP_HAS_NO_INT128
   __u128,
-#endif
   __float,
   __double,
   __long_double,
@@ -75,18 +74,22 @@ visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
     return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__int);
   case __format::__arg_t::__long_long:
     return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__long_long);
-#ifndef _LIBCPP_HAS_NO_INT128
   case __format::__arg_t::__i128:
+#ifndef _LIBCPP_HAS_NO_INT128
     return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__i128);
+#else
+    _LIBCPP_UNREACHABLE();
 #endif
   case __format::__arg_t::__unsigned:
     return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__unsigned);
   case __format::__arg_t::__unsigned_long_long:
     return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis),
                          __arg.__unsigned_long_long);
-#ifndef _LIBCPP_HAS_NO_INT128
   case __format::__arg_t::__u128:
+#ifndef _LIBCPP_HAS_NO_INT128
     return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__u128);
+#else
+   _LIBCPP_UNREACHABLE();
 #endif
   case __format::__arg_t::__float:
     return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__float);

diff  --git a/libcxx/test/libcxx/utilities/format/format.arguments/format.arg/arg_t.compile.pass.cpp b/libcxx/test/libcxx/utilities/format/format.arguments/format.arg/arg_t.compile.pass.cpp
new file mode 100644
index 0000000000000..4e17a61821dce
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/format/format.arguments/format.arg/arg_t.compile.pass.cpp
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: libcpp-has-no-incomplete-format
+
+// <format>
+
+// namespace __format { enum class __arg_t : uint8_t{...}; }
+
+#include <format>
+
+#include <type_traits>
+
+#include "test_macros.h"
+
+static_assert(std::is_same_v<std::underlying_type_t<std::__format::__arg_t>, uint8_t>);
+
+static_assert(uint8_t(std::__format::__arg_t::__none) == 0);
+static_assert(uint8_t(std::__format::__arg_t::__boolean) == 1);
+static_assert(uint8_t(std::__format::__arg_t::__char_type) == 2);
+static_assert(uint8_t(std::__format::__arg_t::__int) == 3);
+static_assert(uint8_t(std::__format::__arg_t::__long_long) == 4);
+static_assert(uint8_t(std::__format::__arg_t::__i128) == 5);
+static_assert(uint8_t(std::__format::__arg_t::__unsigned) == 6);
+static_assert(uint8_t(std::__format::__arg_t::__unsigned_long_long) == 7);
+static_assert(uint8_t(std::__format::__arg_t::__u128) == 8);
+static_assert(uint8_t(std::__format::__arg_t::__float) == 9);
+static_assert(uint8_t(std::__format::__arg_t::__double) == 10);
+static_assert(uint8_t(std::__format::__arg_t::__long_double) == 11);
+static_assert(uint8_t(std::__format::__arg_t::__const_char_type_ptr) == 12);
+static_assert(uint8_t(std::__format::__arg_t::__string_view) == 13);
+static_assert(uint8_t(std::__format::__arg_t::__ptr) == 14);


        


More information about the libcxx-commits mailing list