2 * Copyright (c) Meta Platforms, Inc. and affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/Demangle.h>
19 #include <folly/lang/Pretty.h>
20 #include <folly/portability/GTest.h>
22 using folly::demangle
;
23 using folly::demangle_build_has_cxxabi
;
24 using folly::demangle_build_has_liberty
;
25 using folly::demangle_max_symbol_size
;
26 using folly::pretty_name
;
28 namespace folly_test
{
29 struct ThisIsAVeryLongStructureName
{};
30 } // namespace folly_test
32 class DemangleTest
: public testing::Test
{};
34 TEST_F(DemangleTest
, demangle_return_string
) {
35 using type
= folly_test::ThisIsAVeryLongStructureName
;
36 auto const raw
= typeid(type
).name();
37 auto const expected
= demangle_build_has_cxxabi
? pretty_name
<type
>() : raw
;
38 EXPECT_EQ(expected
, demangle(typeid(type
)));
41 TEST_F(DemangleTest
, demangle_to_buffer
) {
42 using type
= folly_test::ThisIsAVeryLongStructureName
;
43 auto const raw
= typeid(type
).name();
44 auto const expected
= demangle_build_has_liberty
? pretty_name
<type
>() : raw
;
47 std::vector
<char> buf
;
48 buf
.resize(1 + strlen(expected
));
49 EXPECT_EQ(strlen(expected
), demangle(typeid(type
), buf
.data(), buf
.size()));
50 EXPECT_EQ(std::string(expected
), buf
.data());
54 constexpr size_t size
= 10;
55 std::vector
<char> buf
;
57 EXPECT_EQ(strlen(expected
), demangle(typeid(type
), buf
.data(), buf
.size()));
58 EXPECT_EQ(std::string(expected
).substr(0, size
), buf
.data());
62 TEST_F(DemangleTest
, demangle_long_symbol
) {
63 // pretty and demangled names are the same for int but not for size_t
64 // mangling strlen multiplier can be assumed to be at least 4
65 using type
= std::make_integer_sequence
<int, demangle_max_symbol_size
/ 4>;
66 auto raw
= typeid(type
).name();
67 auto choice
= demangle_max_symbol_size
? raw
: pretty_name
<type
>();
69 EXPECT_EQ(std::string(choice
), demangle(raw
).toStdString());
71 auto const expected
= demangle_build_has_liberty
? choice
: raw
;
72 constexpr size_t size
= 15;
73 std::vector
<char> buf
;
75 EXPECT_EQ(strlen(expected
), demangle(raw
, buf
.data(), buf
.size()));
76 EXPECT_EQ(std::string(expected
).substr(0, size
), buf
.data());