Skip to content

Commit 8abca17

Browse files
authored
[libc++] Introduce unversioned namespace macros (#133009)
We've started using `_LIBCPP_BEGIN_NAMESPACE_STD` and `_LIBCPP_END_NAMESPACE_STD` for more than just the namespace for a while now. For example, we're using it to add visibility annotations to types. This works very well and avoids a bunch of annotations, but doesn't work for the few places where we have an unversioned namespace. This adds `_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD` and `_LIBCPP_END_UNVERSIONED_NAMESPACE_STD` to make it simpler to add new annotations consistently across the library as well as making it more explicit that the unversioned namespace is indeed intended.
1 parent 491d3df commit 8abca17

File tree

16 files changed

+49
-42
lines changed

16 files changed

+49
-42
lines changed

libcxx/include/__config

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -577,15 +577,27 @@ typedef __char32_t char32_t;
577577
# define _LIBCPP_POP_EXTENSION_DIAGNOSTICS
578578
# endif
579579

580-
// Inline namespaces are available in Clang/GCC/MSVC regardless of C++ dialect.
581580
// clang-format off
582-
# define _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_PUSH_EXTENSION_DIAGNOSTICS \
583-
namespace _LIBCPP_TYPE_VISIBILITY_DEFAULT std { \
584-
inline namespace _LIBCPP_ABI_NAMESPACE {
585-
# define _LIBCPP_END_NAMESPACE_STD }} _LIBCPP_POP_EXTENSION_DIAGNOSTICS
586581

587-
#define _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL namespace std { namespace experimental {
588-
#define _LIBCPP_END_NAMESPACE_EXPERIMENTAL }}
582+
// The unversioned namespace is used when we want to be ABI compatible with other standard libraries in some way. There
583+
// are two main categories where that's the case:
584+
// - Historically, we have made exception types ABI compatible with libstdc++ to allow throwing them between libstdc++
585+
// and libc++. This is not used anymore for new exception types, since there is no use-case for it anymore.
586+
// - Types and functions which are used by the compiler are in the unversioned namespace, since the compiler has to know
587+
// their mangling without the appropriate declaration in some cases.
588+
// If it's not clear whether using the unversioned namespace is the correct thing to do, it's not. The versioned
589+
// namespace (_LIBCPP_BEGIN_NAMESPACE_STD) should almost always be used.
590+
# define _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD \
591+
_LIBCPP_PUSH_EXTENSION_DIAGNOSTICS namespace _LIBCPP_TYPE_VISIBILITY_DEFAULT std {
592+
593+
# define _LIBCPP_END_UNVERSIONED_NAMESPACE_STD } _LIBCPP_POP_EXTENSION_DIAGNOSTICS
594+
595+
# define _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD inline namespace _LIBCPP_ABI_NAMESPACE {
596+
# define _LIBCPP_END_NAMESPACE_STD } _LIBCPP_END_UNVERSIONED_NAMESPACE_STD
597+
598+
// TODO: This should really be in the versioned namespace
599+
#define _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD namespace experimental {
600+
#define _LIBCPP_END_NAMESPACE_EXPERIMENTAL } _LIBCPP_END_UNVERSIONED_NAMESPACE_STD
589601

590602
#define _LIBCPP_BEGIN_NAMESPACE_LFTS _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL inline namespace fundamentals_v1 {
591603
#define _LIBCPP_END_NAMESPACE_LFTS } _LIBCPP_END_NAMESPACE_EXPERIMENTAL

libcxx/include/__cstddef/byte.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#endif
2020

2121
#if _LIBCPP_STD_VER >= 17
22-
namespace std { // purposefully not versioned
22+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2323

2424
enum class byte : unsigned char {};
2525

@@ -79,7 +79,7 @@ template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
7979
return static_cast<_Integer>(__b);
8080
}
8181

82-
} // namespace std
82+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
8383
#endif // _LIBCPP_STD_VER >= 17
8484

8585
#endif // _LIBCPP___CSTDDEF_BYTE_H

libcxx/include/__exception/exception.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# pragma GCC system_header
2222
#endif
2323

24-
namespace std { // purposefully not using versioning namespace
24+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2525

2626
#if defined(_LIBCPP_ABI_VCRUNTIME) && (!defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS != 0)
2727
// The std::exception class was already included above, but we're explicit about this condition here for clarity.
@@ -89,6 +89,6 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_exception : public exception {
8989
};
9090
#endif // !_LIBCPP_ABI_VCRUNTIME
9191

92-
} // namespace std
92+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
9393

9494
#endif // _LIBCPP___EXCEPTION_EXCEPTION_H

libcxx/include/__exception/exception_ptr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ _LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(
5252

5353
#endif
5454

55-
namespace std { // purposefully not using versioning namespace
55+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
5656

5757
#ifndef _LIBCPP_ABI_MICROSOFT
5858

@@ -171,6 +171,6 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
171171
}
172172

173173
#endif // _LIBCPP_ABI_MICROSOFT
174-
} // namespace std
174+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
175175

176176
#endif // _LIBCPP___EXCEPTION_EXCEPTION_PTR_H

libcxx/include/__exception/nested_exception.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# pragma GCC system_header
2828
#endif
2929

30-
namespace std { // purposefully not using versioning namespace
30+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
3131

3232
class _LIBCPP_EXPORTED_FROM_ABI nested_exception {
3333
exception_ptr __ptr_;
@@ -95,6 +95,6 @@ inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep& __e) {
9595
template <class _Ep, __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value, int> = 0>
9696
inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep&) {}
9797

98-
} // namespace std
98+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
9999

100100
#endif // _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H

libcxx/include/__exception/operations.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# pragma GCC system_header
1616
#endif
1717

18-
namespace std { // purposefully not using versioning namespace
18+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
1919
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS) || \
2020
defined(_LIBCPP_BUILDING_LIBRARY)
2121
using unexpected_handler = void (*)();
@@ -37,6 +37,6 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr;
3737

3838
_LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;
3939
[[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);
40-
} // namespace std
40+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
4141

4242
#endif // _LIBCPP___EXCEPTION_OPERATIONS_H

libcxx/include/__exception/terminate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
# pragma GCC system_header
1616
#endif
1717

18-
namespace std { // purposefully not using versioning namespace
18+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
1919
[[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void terminate() _NOEXCEPT;
20-
} // namespace std
20+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
2121

2222
#endif // _LIBCPP___EXCEPTION_TERMINATE_H

libcxx/include/__fwd/byte.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#endif
1717

1818
#if _LIBCPP_STD_VER >= 17
19-
namespace std { // purposefully not versioned
19+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2020

2121
enum class byte : unsigned char;
2222

23-
} // namespace std
23+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
2424
#endif // _LIBCPP_STD_VER >= 17
2525

2626
#endif // _LIBCPP___FWD_BYTE_H

libcxx/include/__new/align_val_t.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
# pragma GCC system_header
1717
#endif
1818

19-
// purposefully not using versioning namespace
20-
namespace std {
19+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2120
#if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION && !defined(_LIBCPP_ABI_VCRUNTIME)
2221
# ifndef _LIBCPP_CXX03_LANG
2322
enum class align_val_t : size_t {};
2423
# else
2524
enum align_val_t { __zero = 0, __max = (size_t)-1 };
2625
# endif
2726
#endif
28-
} // namespace std
27+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
2928

3029
#endif // _LIBCPP___NEW_ALIGN_VAL_T_H

libcxx/include/__new/destroying_delete_t.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
#endif
1717

1818
#if _LIBCPP_STD_VER >= 20
19-
// purposefully not using versioning namespace
20-
namespace std {
19+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2120
// Enable the declaration even if the compiler doesn't support the language
2221
// feature.
2322
struct destroying_delete_t {
2423
explicit destroying_delete_t() = default;
2524
};
2625
inline constexpr destroying_delete_t destroying_delete{};
27-
} // namespace std
26+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
2827
#endif
2928

3029
#endif // _LIBCPP___NEW_DESTROYING_DELETE_T_H

libcxx/include/__new/exceptions.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
# pragma GCC system_header
1818
#endif
1919

20-
// purposefully not using versioning namespace
21-
namespace std {
20+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2221
#if !defined(_LIBCPP_ABI_VCRUNTIME)
2322

2423
class _LIBCPP_EXPORTED_FROM_ABI bad_alloc : public exception {
@@ -69,6 +68,6 @@ class bad_array_new_length : public bad_alloc {
6968
_LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");
7069
#endif
7170
}
72-
} // namespace std
71+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
7372

7473
#endif // _LIBCPP___NEW_EXCEPTIONS_H

libcxx/include/__new/new_handler.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
#if defined(_LIBCPP_ABI_VCRUNTIME)
1919
# include <new.h>
2020
#else
21-
// purposefully not using versioning namespace
22-
namespace std {
21+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2322
typedef void (*new_handler)();
2423
_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
2524
_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
26-
} // namespace std
25+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
2726
#endif // _LIBCPP_ABI_VCRUNTIME
2827

2928
#endif // _LIBCPP___NEW_NEW_HANDLER_H

libcxx/include/__new/nothrow_t.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
#if defined(_LIBCPP_ABI_VCRUNTIME)
1919
# include <new.h>
2020
#else
21-
// purposefully not using versioning namespace
22-
namespace std {
21+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2322
struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t {
2423
explicit nothrow_t() = default;
2524
};
2625
extern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow;
27-
} // namespace std
26+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
2827
#endif // _LIBCPP_ABI_VCRUNTIME
2928

3029
#endif // _LIBCPP___NEW_NOTHROW_T_H

libcxx/include/any

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ namespace std {
119119
_LIBCPP_PUSH_MACROS
120120
# include <__undef_macros>
121121

122-
namespace std {
122+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
123123
class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast {
124124
public:
125125
const char* what() const _NOEXCEPT override;
126126
};
127-
} // namespace std
127+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
128128

129129
_LIBCPP_BEGIN_NAMESPACE_STD
130130

libcxx/include/typeinfo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public:
354354

355355
# if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
356356

357-
namespace std {
357+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
358358

359359
class bad_cast : public exception {
360360
public:
@@ -372,7 +372,7 @@ private:
372372
bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {}
373373
};
374374

375-
} // namespace std
375+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
376376

377377
# endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
378378

libcxx/include/variant

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,14 @@ namespace std {
283283
_LIBCPP_PUSH_MACROS
284284
# include <__undef_macros>
285285

286-
namespace std { // explicitly not using versioning namespace
286+
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
287287

288288
class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS bad_variant_access : public exception {
289289
public:
290290
const char* what() const _NOEXCEPT override;
291291
};
292292

293-
} // namespace std
293+
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
294294

295295
_LIBCPP_BEGIN_NAMESPACE_STD
296296

0 commit comments

Comments
 (0)