xref: /llvm-project/lldb/unittests/Core/RichManglingContextTest.cpp (revision fa52788b7a6da1eab9fec0f2db5f74a8db555196)
1 //===-- RichManglingContextTest.cpp ---------------------------------------===//
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 #include "lldb/Core/RichManglingContext.h"
10 
11 #include "lldb/Utility/ConstString.h"
12 
13 #include "gtest/gtest.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
TEST(RichManglingContextTest,Basic)18 TEST(RichManglingContextTest, Basic) {
19   RichManglingContext RMC;
20   ConstString mangled("_ZN3foo3barEv");
21 
22   EXPECT_TRUE(RMC.FromItaniumName(mangled));
23   EXPECT_FALSE(RMC.IsCtorOrDtor());
24   EXPECT_EQ("foo", RMC.ParseFunctionDeclContextName());
25   EXPECT_EQ("bar", RMC.ParseFunctionBaseName());
26   EXPECT_EQ("foo::bar()", RMC.ParseFullName());
27 }
28 
TEST(RichManglingContextTest,FromCxxMethodName)29 TEST(RichManglingContextTest, FromCxxMethodName) {
30   RichManglingContext ItaniumRMC;
31   ConstString mangled("_ZN3foo3barEv");
32   EXPECT_TRUE(ItaniumRMC.FromItaniumName(mangled));
33 
34   RichManglingContext CxxMethodRMC;
35   ConstString demangled("foo::bar()");
36   EXPECT_TRUE(CxxMethodRMC.FromCxxMethodName(demangled));
37 
38   EXPECT_EQ(ItaniumRMC.IsCtorOrDtor(), CxxMethodRMC.IsCtorOrDtor());
39   EXPECT_EQ(ItaniumRMC.ParseFunctionDeclContextName(),
40             CxxMethodRMC.ParseFunctionDeclContextName());
41   EXPECT_EQ(ItaniumRMC.ParseFunctionBaseName(),
42             CxxMethodRMC.ParseFunctionBaseName());
43   EXPECT_EQ(ItaniumRMC.ParseFullName(), CxxMethodRMC.ParseFullName());
44 
45   // Construct with a random name.
46   {
47     RichManglingContext CxxMethodRMC;
48     EXPECT_TRUE(CxxMethodRMC.FromCxxMethodName(ConstString("X")));
49   }
50 
51   // Construct with a function without a context.
52   {
53     RichManglingContext CxxMethodRMC;
54     EXPECT_TRUE(CxxMethodRMC.FromCxxMethodName(
55         ConstString("void * operator new(unsigned __int64)")));
56 
57     // We expect its context is empty.
58     EXPECT_TRUE(CxxMethodRMC.ParseFunctionDeclContextName().empty());
59   }
60 }
61 
TEST(RichManglingContextTest,SwitchProvider)62 TEST(RichManglingContextTest, SwitchProvider) {
63   RichManglingContext RMC;
64   llvm::StringRef mangled = "_ZN3foo3barEv";
65   llvm::StringRef demangled = "foo::bar()";
66 
67   EXPECT_TRUE(RMC.FromItaniumName(ConstString(mangled)));
68   EXPECT_EQ("foo::bar()", RMC.ParseFullName());
69 
70   EXPECT_TRUE(RMC.FromCxxMethodName(ConstString(demangled)));
71   EXPECT_EQ("foo::bar()", RMC.ParseFullName());
72 
73   EXPECT_TRUE(RMC.FromItaniumName(ConstString(mangled)));
74   EXPECT_EQ("foo::bar()", RMC.ParseFullName());
75 }
76 
TEST(RichManglingContextTest,IPDRealloc)77 TEST(RichManglingContextTest, IPDRealloc) {
78   // The demangled name should fit into the Itanium default buffer.
79   const char *ShortMangled = "_ZN3foo3barEv";
80 
81   // The demangled name for this will certainly not fit into the default buffer.
82   const char *LongMangled =
83       "_ZNK3shk6detail17CallbackPublisherIZNS_5ThrowERKNSt15__exception_"
84       "ptr13exception_ptrEEUlOT_E_E9SubscribeINS0_9ConcatMapINS0_"
85       "18CallbackSubscriberIZNS_6GetAllIiNS1_IZZNS_9ConcatMapIZNS_6ConcatIJNS1_"
86       "IZZNS_3MapIZZNS_7IfEmptyIS9_EEDaS7_ENKUlS6_E_clINS1_IZZNS_4TakeIiEESI_"
87       "S7_ENKUlS6_E_clINS1_IZZNS_6FilterIZNS_9ElementAtEmEUlS7_E_EESI_S7_"
88       "ENKUlS6_E_clINS1_IZZNSL_ImEESI_S7_ENKUlS6_E_clINS1_IZNS_4FromINS0_"
89       "22InfiniteRangeContainerIiEEEESI_S7_EUlS7_E_EEEESI_S6_EUlS7_E_EEEESI_S6_"
90       "EUlS7_E_EEEESI_S6_EUlS7_E_EEEESI_S6_EUlS7_E_EESI_S7_ENKUlS6_E_clIS14_"
91       "EESI_S6_EUlS7_E_EERNS1_IZZNSH_IS9_EESI_S7_ENKSK_IS14_EESI_S6_EUlS7_E0_"
92       "EEEEESI_DpOT_EUlS7_E_EESI_S7_ENKUlS6_E_clINS1_IZNS_5StartIJZNS_"
93       "4JustIJS19_S1C_EEESI_S1F_EUlvE_ZNS1K_IJS19_S1C_EEESI_S1F_EUlvE0_EEESI_"
94       "S1F_EUlS7_E_EEEESI_S6_EUlS7_E_EEEESt6vectorIS6_SaIS6_EERKT0_NS_"
95       "12ElementCountEbEUlS7_E_ZNSD_IiS1Q_EES1T_S1W_S1X_bEUlOS3_E_ZNSD_IiS1Q_"
96       "EES1T_S1W_S1X_bEUlvE_EES1G_S1O_E25ConcatMapValuesSubscriberEEEDaS7_";
97 
98   RichManglingContext RMC;
99 
100   // Demangle the short one.
101   EXPECT_TRUE(RMC.FromItaniumName(ConstString(ShortMangled)));
102   const char *ShortDemangled = RMC.ParseFullName().data();
103 
104   // Demangle the long one.
105   EXPECT_TRUE(RMC.FromItaniumName(ConstString(LongMangled)));
106   const char *LongDemangled = RMC.ParseFullName().data();
107 
108   // Make sure a new buffer was allocated or the default buffer was extended.
109   bool AllocatedNewBuffer = (ShortDemangled != LongDemangled);
110   bool ExtendedExistingBuffer = (strlen(LongDemangled) > 2048);
111   EXPECT_TRUE(AllocatedNewBuffer || ExtendedExistingBuffer);
112 }
113