-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[libc++] Add test to ensure that the mangling of types stays the same #143556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-libcxx Author: Nikolas Klauser (philnik777) ChangesFull diff: https://github.com/llvm/llvm-project/pull/143556.diff 1 Files Affected:
diff --git a/libcxx/test/libcxx/mangled_names.pass.cpp b/libcxx/test/libcxx/mangled_names.pass.cpp
new file mode 100644
index 0000000000000..b74f08e95ffa4
--- /dev/null
+++ b/libcxx/test/libcxx/mangled_names.pass.cpp
@@ -0,0 +1,69 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// Make sure that the mangling of our public types stays the same
+
+#include <cassert>
+#include <charconv>
+#include <iostream>
+#include <map>
+#include <typeinfo>
+#include <string_view>
+
+template <class>
+struct mangling {};
+
+struct test_struct {};
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+struct ns_mangling {};
+_LIBCPP_END_NAMESPACE_STD
+
+namespace std::__name {
+struct ns_mangling {};
+} // namespace std::__name
+
+namespace std::__long_name_to_make_sure_multiple_digits_work {
+struct ns_mangling {};
+} // namespace std::__long_name_to_make_sure_multiple_digits_work
+
+std::string get_std_inline_namespace_mangling(const std::type_info& info) {
+ std::string name = info.name();
+ assert(name.starts_with("NSt"));
+ unsigned name_len;
+ auto res = std::from_chars(name.data() + 3, name.data() + name.size(), name_len);
+ assert(res.ec == std::errc{});
+ return std::move(name).substr(0, (res.ptr + name_len) - name.data());
+}
+
+void expect_mangling(const std::type_info& info, std::string expected_name) {
+ if (expected_name != info.name())
+ std::__libcpp_verbose_abort("Expected: '%s'\n Got: '%s'\n", expected_name.c_str(), info.name());
+}
+
+#define EXPECT_MANGLING(expected_mangling, ...) expect_mangling(typeid(__VA_ARGS__), expected_mangling)
+
+// Mangling names are really long, but splitting it up into multiple lines doesn't make it any more readable
+// clang-format off
+int main(int, char**) {
+ // self-test inline namespace recovery
+ assert(get_std_inline_namespace_mangling(typeid(std::__name::ns_mangling)) == "NSt6__name");
+ 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");
+
+ // selftest
+ EXPECT_MANGLING("11test_struct", test_struct);
+
+ std::string ns_std = get_std_inline_namespace_mangling(typeid(std::ns_mangling));
+
+ // std::map
+ EXPECT_MANGLING(ns_std + "3mapIiiNS_4lessIiEENS_9allocatorINS_4pairIKiiEEEEEE", std::map<int, int>);
+ EXPECT_MANGLING(ns_std + "14__map_iteratorINS_15__tree_iteratorINS_12__value_typeIiiEEPNS_11__tree_nodeIS3_PvEElEEEE", std::map<int, int>::iterator);
+ 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);
+
+ return 0;
+}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
f9ae93f
to
fbafd60
Compare
// We're using `std::from_chars` in this test | ||
// UNSUPPORTED: c++03, c++11, c++14 | ||
|
||
// Make sure that the mangling of our public types stays the same |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Make sure that the mangling of our public types stays the same | |
// This test pins down the mangling of our public types to ensure that we don't | |
// change it unintentionally, which is an ABI break. |
|
||
std::string ns_std = get_std_inline_namespace_mangling(typeid(std::ns_mangling)); | ||
|
||
// std::map |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the plan to manually expand this to all public types in the library? That seems like something that'll quickly get out of hand and out of sync. I thought the idea was to create an automated test for this somehow (maybe clang-tidy?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point I'd like to add an automated test, yes. I'm not quite sure how to do that yet though.
For now my plan is to just pin down manglings where we think it's easy to mess up. I don't expect we'd miss a mangling change in most places, so I don't think it makes a ton of sense to add stuff like vector::iterator
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future, I think we should write this test consistently with how we do the rest of ABI list testing -- when we rework how that works. For now, the current approach seems OK to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal here seems similar to the symbol list tests.
We want to ensure that a users program which exposes the mangled name of std::foo
as part of one of their symbols continues to link when compiler before & after a libc++ change.
It seems to me that given the specification of std::type_info::name
, that it's not the most suitable tool for the job. In particular:
Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.
I would consider simply generating a big symbol name, containing all of the types we want to test the names of, and then ensuring code generated before a given change can link to code generated after the change.
For example:
#include <string>
#include <vector>
#include <tuple>
// Compile the definition with the headers in trunk
// RUN: %{cxx} -c -cxx-isystem %{trunk-headers} -o %t.o -DDEFINE_TARGET
// Then compile the main
// RUN: %{cxx} -cxx-isystem %{headers-under-test} -o a.out %t.o
// RUN: ./a.out
template <class ...> struct TypeList {};
using TypesUnderTest =
TypeList<
std::vector<int>,
std::vector<int>::iterator,
std::tuple<int, int&>
>;
void target(TypesUnderTest);
#ifdef DEFINE_TARGET
void target(TypesUnderTest) {}
#else
int main() {
target({});
}
#endif
This approach sort-of avoids having golden files checked in, which is a pain since they need manual care whenever we add new configurations or to regenerate them. It instead expects that we have two versions of the headers available, and treats one of those versions as "golden" (which is sort of what we want I think).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM w/ comments applies and CI green.
Windows should fail (please ensure it does, and then add UNSUPPORTED
).
|
||
std::string ns_std = get_std_inline_namespace_mangling(typeid(std::ns_mangling)); | ||
|
||
// std::map |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future, I think we should write this test consistently with how we do the rest of ABI list testing -- when we rework how that works. For now, the current approach seems OK to me.
dda7d1b
to
cec0e0c
Compare
cec0e0c
to
643311c
Compare
//===----------------------------------------------------------------------===// | ||
|
||
// We're using `string::starts_with` in this test | ||
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems more important that we test the manglings are consistent across dialects, rather than use starts_with
.
No description provided.