Skip to content

Commit f9ae93f

Browse files
committed
[libc++] Add test to ensure that the mangling of types stays the same
1 parent 4fb81f1 commit f9ae93f

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// Make sure that the mangling of our public types stays the same
10+
11+
#include <cassert>
12+
#include <charconv>
13+
#include <iostream>
14+
#include <map>
15+
#include <typeinfo>
16+
#include <string_view>
17+
18+
template <class>
19+
struct mangling {};
20+
21+
struct test_struct {};
22+
23+
_LIBCPP_BEGIN_NAMESPACE_STD
24+
struct ns_mangling {};
25+
_LIBCPP_END_NAMESPACE_STD
26+
27+
namespace std {
28+
namespace __name {
29+
struct ns_mangling {};
30+
} // namespace __name
31+
} // namespace std
32+
33+
namespace std::__long_name_to_make_sure_multiple_digits_work {
34+
struct ns_mangling {};
35+
} // namespace std::__long_name_to_make_sure_multiple_digits_work
36+
37+
std::string get_std_inline_namespace_mangling(const std::type_info& info) {
38+
std::string name = info.name();
39+
assert(name.starts_with("NSt"));
40+
unsigned name_len;
41+
auto res = std::from_chars(name.data() + 3, name.data() + name.size(), name_len);
42+
assert(res.ec == std::errc{});
43+
return std::move(name).substr(0, (res.ptr + name_len) - name.data());
44+
}
45+
46+
void expect_mangling(const std::type_info& info, std::string expected_name) {
47+
if (expected_name != info.name())
48+
std::__libcpp_verbose_abort("Expected: '%s'\n Got: '%s'\n", expected_name.c_str(), info.name());
49+
}
50+
51+
#define EXPECT_MANGLING(expected_mangling, ...) expect_mangling(typeid(__VA_ARGS__), expected_mangling)
52+
53+
// Mangling names are really long, but splitting it up into multiple lines doesn't make it any more readable
54+
// clang-format off
55+
int main(int, char**) {
56+
// self-test inline namespace recovery
57+
assert(get_std_inline_namespace_mangling(typeid(std::__name::ns_mangling)) == "NSt6__name");
58+
assert(get_std_inline_namespace_mangling(typeid(std::__long_name_to_make_sure_multiple_digits_work::ns_mangling)) == "NSt45__long_name_to_make_sure_multiple_digits_work");
59+
60+
// selftest
61+
EXPECT_MANGLING("11test_struct", test_struct);
62+
63+
std::string ns_std = get_std_inline_namespace_mangling(typeid(std::ns_mangling));
64+
65+
// std::map
66+
EXPECT_MANGLING(ns_std + "3mapIiiNS_4lessIiEENS_9allocatorINS_4pairIKiiEEEEEE", std::map<int, int>);
67+
EXPECT_MANGLING(ns_std + "14__map_iteratorINS_15__tree_iteratorINS_12__value_typeIiiEEPNS_11__tree_nodeIS3_PvEElEEEE", std::map<int, int>::iterator);
68+
EXPECT_MANGLING(ns_std + "20__map_const_iteratorINS_21__tree_const_iteratorINS_12__value_typeIiiEEPNS_11__tree_nodeIS3_PvEElEEEE", std::map<int, int>::const_iterator);
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)