xref: /llvm-project/lldb/unittests/Core/RichManglingContextTest.cpp (revision fa52788b7a6da1eab9fec0f2db5f74a8db555196)
180814287SRaphael Isemann //===-- RichManglingContextTest.cpp ---------------------------------------===//
2f1a98df6SStefan Granitz //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f1a98df6SStefan Granitz //
7f1a98df6SStefan Granitz //===----------------------------------------------------------------------===//
8f1a98df6SStefan Granitz 
9f1a98df6SStefan Granitz #include "lldb/Core/RichManglingContext.h"
10f1a98df6SStefan Granitz 
11f1a98df6SStefan Granitz #include "lldb/Utility/ConstString.h"
12f1a98df6SStefan Granitz 
13f1a98df6SStefan Granitz #include "gtest/gtest.h"
14f1a98df6SStefan Granitz 
15f1a98df6SStefan Granitz using namespace lldb;
16f1a98df6SStefan Granitz using namespace lldb_private;
17f1a98df6SStefan Granitz 
TEST(RichManglingContextTest,Basic)18f1a98df6SStefan Granitz TEST(RichManglingContextTest, Basic) {
19f1a98df6SStefan Granitz   RichManglingContext RMC;
20f1a98df6SStefan Granitz   ConstString mangled("_ZN3foo3barEv");
21*fa52788bSJonas Devlieghere 
22f1a98df6SStefan Granitz   EXPECT_TRUE(RMC.FromItaniumName(mangled));
23f1a98df6SStefan Granitz   EXPECT_FALSE(RMC.IsCtorOrDtor());
24*fa52788bSJonas Devlieghere   EXPECT_EQ("foo", RMC.ParseFunctionDeclContextName());
25*fa52788bSJonas Devlieghere   EXPECT_EQ("bar", RMC.ParseFunctionBaseName());
26*fa52788bSJonas Devlieghere   EXPECT_EQ("foo::bar()", RMC.ParseFullName());
27f1a98df6SStefan Granitz }
28f1a98df6SStefan Granitz 
TEST(RichManglingContextTest,FromCxxMethodName)29f1a98df6SStefan Granitz TEST(RichManglingContextTest, FromCxxMethodName) {
30f1a98df6SStefan Granitz   RichManglingContext ItaniumRMC;
31f1a98df6SStefan Granitz   ConstString mangled("_ZN3foo3barEv");
32f1a98df6SStefan Granitz   EXPECT_TRUE(ItaniumRMC.FromItaniumName(mangled));
33f1a98df6SStefan Granitz 
34f1a98df6SStefan Granitz   RichManglingContext CxxMethodRMC;
35f1a98df6SStefan Granitz   ConstString demangled("foo::bar()");
36f1a98df6SStefan Granitz   EXPECT_TRUE(CxxMethodRMC.FromCxxMethodName(demangled));
37f1a98df6SStefan Granitz 
38*fa52788bSJonas Devlieghere   EXPECT_EQ(ItaniumRMC.IsCtorOrDtor(), CxxMethodRMC.IsCtorOrDtor());
39*fa52788bSJonas Devlieghere   EXPECT_EQ(ItaniumRMC.ParseFunctionDeclContextName(),
40*fa52788bSJonas Devlieghere             CxxMethodRMC.ParseFunctionDeclContextName());
41*fa52788bSJonas Devlieghere   EXPECT_EQ(ItaniumRMC.ParseFunctionBaseName(),
42*fa52788bSJonas Devlieghere             CxxMethodRMC.ParseFunctionBaseName());
43*fa52788bSJonas Devlieghere   EXPECT_EQ(ItaniumRMC.ParseFullName(), CxxMethodRMC.ParseFullName());
442fc9c3b0SAaron Smith 
452fc9c3b0SAaron Smith   // Construct with a random name.
462fc9c3b0SAaron Smith   {
472fc9c3b0SAaron Smith     RichManglingContext CxxMethodRMC;
482fc9c3b0SAaron Smith     EXPECT_TRUE(CxxMethodRMC.FromCxxMethodName(ConstString("X")));
492fc9c3b0SAaron Smith   }
502fc9c3b0SAaron Smith 
512fc9c3b0SAaron Smith   // Construct with a function without a context.
522fc9c3b0SAaron Smith   {
532fc9c3b0SAaron Smith     RichManglingContext CxxMethodRMC;
542fc9c3b0SAaron Smith     EXPECT_TRUE(CxxMethodRMC.FromCxxMethodName(
552fc9c3b0SAaron Smith         ConstString("void * operator new(unsigned __int64)")));
562fc9c3b0SAaron Smith 
572fc9c3b0SAaron Smith     // We expect its context is empty.
58*fa52788bSJonas Devlieghere     EXPECT_TRUE(CxxMethodRMC.ParseFunctionDeclContextName().empty());
592fc9c3b0SAaron Smith   }
60f1a98df6SStefan Granitz }
61f1a98df6SStefan Granitz 
TEST(RichManglingContextTest,SwitchProvider)62f1a98df6SStefan Granitz TEST(RichManglingContextTest, SwitchProvider) {
63f1a98df6SStefan Granitz   RichManglingContext RMC;
64f1a98df6SStefan Granitz   llvm::StringRef mangled = "_ZN3foo3barEv";
65f1a98df6SStefan Granitz   llvm::StringRef demangled = "foo::bar()";
66f1a98df6SStefan Granitz 
67f1a98df6SStefan Granitz   EXPECT_TRUE(RMC.FromItaniumName(ConstString(mangled)));
68*fa52788bSJonas Devlieghere   EXPECT_EQ("foo::bar()", RMC.ParseFullName());
69f1a98df6SStefan Granitz 
70f1a98df6SStefan Granitz   EXPECT_TRUE(RMC.FromCxxMethodName(ConstString(demangled)));
71*fa52788bSJonas Devlieghere   EXPECT_EQ("foo::bar()", RMC.ParseFullName());
72f1a98df6SStefan Granitz 
73f1a98df6SStefan Granitz   EXPECT_TRUE(RMC.FromItaniumName(ConstString(mangled)));
74*fa52788bSJonas Devlieghere   EXPECT_EQ("foo::bar()", RMC.ParseFullName());
75f1a98df6SStefan Granitz }
76f1a98df6SStefan Granitz 
TEST(RichManglingContextTest,IPDRealloc)77f1a98df6SStefan Granitz TEST(RichManglingContextTest, IPDRealloc) {
78f1a98df6SStefan Granitz   // The demangled name should fit into the Itanium default buffer.
79d0514164SStefan Granitz   const char *ShortMangled = "_ZN3foo3barEv";
80f1a98df6SStefan Granitz 
81f1a98df6SStefan Granitz   // The demangled name for this will certainly not fit into the default buffer.
82d0514164SStefan Granitz   const char *LongMangled =
83f1a98df6SStefan Granitz       "_ZNK3shk6detail17CallbackPublisherIZNS_5ThrowERKNSt15__exception_"
84f1a98df6SStefan Granitz       "ptr13exception_ptrEEUlOT_E_E9SubscribeINS0_9ConcatMapINS0_"
85f1a98df6SStefan Granitz       "18CallbackSubscriberIZNS_6GetAllIiNS1_IZZNS_9ConcatMapIZNS_6ConcatIJNS1_"
86f1a98df6SStefan Granitz       "IZZNS_3MapIZZNS_7IfEmptyIS9_EEDaS7_ENKUlS6_E_clINS1_IZZNS_4TakeIiEESI_"
87f1a98df6SStefan Granitz       "S7_ENKUlS6_E_clINS1_IZZNS_6FilterIZNS_9ElementAtEmEUlS7_E_EESI_S7_"
88f1a98df6SStefan Granitz       "ENKUlS6_E_clINS1_IZZNSL_ImEESI_S7_ENKUlS6_E_clINS1_IZNS_4FromINS0_"
89f1a98df6SStefan Granitz       "22InfiniteRangeContainerIiEEEESI_S7_EUlS7_E_EEEESI_S6_EUlS7_E_EEEESI_S6_"
90f1a98df6SStefan Granitz       "EUlS7_E_EEEESI_S6_EUlS7_E_EEEESI_S6_EUlS7_E_EESI_S7_ENKUlS6_E_clIS14_"
91f1a98df6SStefan Granitz       "EESI_S6_EUlS7_E_EERNS1_IZZNSH_IS9_EESI_S7_ENKSK_IS14_EESI_S6_EUlS7_E0_"
92f1a98df6SStefan Granitz       "EEEEESI_DpOT_EUlS7_E_EESI_S7_ENKUlS6_E_clINS1_IZNS_5StartIJZNS_"
93f1a98df6SStefan Granitz       "4JustIJS19_S1C_EEESI_S1F_EUlvE_ZNS1K_IJS19_S1C_EEESI_S1F_EUlvE0_EEESI_"
94f1a98df6SStefan Granitz       "S1F_EUlS7_E_EEEESI_S6_EUlS7_E_EEEESt6vectorIS6_SaIS6_EERKT0_NS_"
95f1a98df6SStefan Granitz       "12ElementCountEbEUlS7_E_ZNSD_IiS1Q_EES1T_S1W_S1X_bEUlOS3_E_ZNSD_IiS1Q_"
96f1a98df6SStefan Granitz       "EES1T_S1W_S1X_bEUlvE_EES1G_S1O_E25ConcatMapValuesSubscriberEEEDaS7_";
97f1a98df6SStefan Granitz 
98f1a98df6SStefan Granitz   RichManglingContext RMC;
99f1a98df6SStefan Granitz 
100d0514164SStefan Granitz   // Demangle the short one.
101d0514164SStefan Granitz   EXPECT_TRUE(RMC.FromItaniumName(ConstString(ShortMangled)));
102*fa52788bSJonas Devlieghere   const char *ShortDemangled = RMC.ParseFullName().data();
103f1a98df6SStefan Granitz 
104d0514164SStefan Granitz   // Demangle the long one.
105d0514164SStefan Granitz   EXPECT_TRUE(RMC.FromItaniumName(ConstString(LongMangled)));
106*fa52788bSJonas Devlieghere   const char *LongDemangled = RMC.ParseFullName().data();
107f1a98df6SStefan Granitz 
108d0514164SStefan Granitz   // Make sure a new buffer was allocated or the default buffer was extended.
109d0514164SStefan Granitz   bool AllocatedNewBuffer = (ShortDemangled != LongDemangled);
110d0514164SStefan Granitz   bool ExtendedExistingBuffer = (strlen(LongDemangled) > 2048);
111d0514164SStefan Granitz   EXPECT_TRUE(AllocatedNewBuffer || ExtendedExistingBuffer);
112f1a98df6SStefan Granitz }
113