1 //===-- MangledTest.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
10 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
11 #include "TestingSupport/SubsystemRAII.h"
12 #include "TestingSupport/TestUtilities.h"
14 #include "lldb/Core/Mangled.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Host/FileSystem.h"
18 #include "lldb/Host/HostInfo.h"
19 #include "lldb/Symbol/SymbolContext.h"
21 #include "llvm/Support/FileUtilities.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Testing/Support/Error.h"
26 #include "gtest/gtest.h"
29 using namespace lldb_private
;
31 TEST(MangledTest
, ResultForValidName
) {
32 ConstString
MangledName("_ZN1a1b1cIiiiEEvm");
33 Mangled
TheMangled(MangledName
);
34 ConstString TheDemangled
= TheMangled
.GetDemangledName();
36 ConstString
ExpectedResult("void a::b::c<int, int, int>(unsigned long)");
37 EXPECT_STREQ(ExpectedResult
.GetCString(), TheDemangled
.GetCString());
40 TEST(MangledTest
, ResultForBlockInvocation
) {
41 ConstString
MangledName("___Z1fU13block_pointerFviE_block_invoke");
42 Mangled
TheMangled(MangledName
);
43 ConstString TheDemangled
= TheMangled
.GetDemangledName();
45 ConstString
ExpectedResult(
46 "invocation function for block in f(void (int) block_pointer)");
47 EXPECT_STREQ(ExpectedResult
.GetCString(), TheDemangled
.GetCString());
50 TEST(MangledTest
, EmptyForInvalidName
) {
51 ConstString
MangledName("_ZN1a1b1cmxktpEEvm");
52 Mangled
TheMangled(MangledName
);
53 ConstString TheDemangled
= TheMangled
.GetDemangledName();
55 EXPECT_STREQ("", TheDemangled
.GetCString());
58 TEST(MangledTest
, ResultForValidRustV0Name
) {
59 ConstString
mangled_name("_RNvC1a4main");
60 Mangled
the_mangled(mangled_name
);
61 ConstString the_demangled
= the_mangled
.GetDemangledName();
63 ConstString
expected_result("a::main");
64 EXPECT_STREQ(expected_result
.GetCString(), the_demangled
.GetCString());
67 TEST(MangledTest
, EmptyForInvalidRustV0Name
) {
68 ConstString
mangled_name("_RRR");
69 Mangled
the_mangled(mangled_name
);
70 ConstString the_demangled
= the_mangled
.GetDemangledName();
72 EXPECT_STREQ("", the_demangled
.GetCString());
75 TEST(MangledTest
, ResultForValidDLangName
) {
76 ConstString
mangled_name("_Dmain");
77 Mangled
the_mangled(mangled_name
);
78 ConstString the_demangled
= the_mangled
.GetDemangledName();
80 ConstString
expected_result("D main");
81 EXPECT_STREQ(expected_result
.GetCString(), the_demangled
.GetCString());
84 TEST(MangledTest
, EmptyForInvalidDLangName
) {
85 ConstString
mangled_name("_DDD");
86 Mangled
the_mangled(mangled_name
);
87 ConstString the_demangled
= the_mangled
.GetDemangledName();
89 EXPECT_STREQ("", the_demangled
.GetCString());
92 TEST(MangledTest
, RecognizeSwiftMangledNames
) {
93 llvm::StringRef valid_swift_mangled_names
[] = {
94 "_TtC4main7MyClass", // Mangled objc class name
95 "_TtP4main3Foo_", // Mangld objc protocol name
96 "$s4main3BarCACycfC", // Mangled name
97 "_$s4main3BarCACycfC", // Mangled name with leading underscore
98 "$S4main3BarCACycfC", // Older swift mangled name
99 "_$S4main3BarCACycfC", // Older swift mangled name
100 // with leading underscore
101 // Mangled swift filename
102 "@__swiftmacro_4main16FunVariableNames9OptionSetfMm_.swift",
105 for (llvm::StringRef mangled
: valid_swift_mangled_names
)
106 EXPECT_EQ(Mangled::GetManglingScheme(mangled
),
107 Mangled::eManglingSchemeSwift
);
110 TEST(MangledTest
, BoolConversionOperator
) {
112 ConstString
MangledName("_ZN1a1b1cIiiiEEvm");
113 Mangled
TheMangled(MangledName
);
114 EXPECT_EQ(true, bool(TheMangled
));
115 EXPECT_EQ(false, !TheMangled
);
118 ConstString
UnmangledName("puts");
119 Mangled
TheMangled(UnmangledName
);
120 EXPECT_EQ(true, bool(TheMangled
));
121 EXPECT_EQ(false, !TheMangled
);
124 Mangled TheMangled
{};
125 EXPECT_EQ(false, bool(TheMangled
));
126 EXPECT_EQ(true, !TheMangled
);
130 TEST(MangledTest
, NameIndexes_FindFunctionSymbols
) {
131 SubsystemRAII
<FileSystem
, HostInfo
, ObjectFileELF
, SymbolFileSymtab
>
134 auto ExpectedFile
= TestFile::fromYaml(R
"(
144 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
145 AddressAlign: 0x0000000000000010
149 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
150 Address: 0x0000000000000010
151 AddressAlign: 0x0000000000000010
155 Flags: [ SHF_WRITE, SHF_ALLOC ]
156 Address: 0x00000000000000A8
157 AddressAlign: 0x0000000000000004
162 Section: .anothertext
163 Value: 0x0000000000000045
167 Section: .anothertext
168 Value: 0x0000000000000010
169 Size: 0x000000000000003F
174 Size: 0x000000000000000D
176 - Name: puts@GLIBC_2.5
179 Size: 0x000000000000000D
181 - Name: puts@GLIBC_2.6
184 Size: 0x000000000000000D
186 - Name: _Z5annotv@VERSION3
189 Size: 0x000000000000000D
194 Size: 0x000000000000000D
199 Size: 0x000000000000000D
204 Size: 0x000000000000000D
206 - Name: _ZGVZN4llvm4dbgsEvE7thestrm
209 Size: 0x000000000000000D
211 - Name: _ZZN4llvm4dbgsEvE7thestrm
214 Size: 0x000000000000000D
216 - Name: _ZTVN5clang4DeclE
219 Size: 0x000000000000000D
224 Size: 0x000000000000000D
226 - Name: +[B ObjCbar(WithCategory)]
229 Size: 0x000000000000000D
231 - Name: _Z12undemangableEvx42
234 Size: 0x000000000000000D
238 ASSERT_THAT_EXPECTED(ExpectedFile
, llvm::Succeeded());
240 auto M
= std::make_shared
<Module
>(ExpectedFile
->moduleSpec());
242 auto Count
= [M
](const char *Name
, FunctionNameType Type
) -> int {
243 SymbolContextList SymList
;
244 M
->FindFunctionSymbols(ConstString(Name
), Type
, SymList
);
245 return SymList
.GetSize();
249 EXPECT_EQ(1, Count("main", eFunctionNameTypeFull
));
250 EXPECT_EQ(1, Count("main", eFunctionNameTypeBase
));
251 EXPECT_EQ(0, Count("main", eFunctionNameTypeMethod
));
254 EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeFull
));
255 EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeBase
));
256 EXPECT_EQ(1, Count("foo", eFunctionNameTypeBase
));
257 EXPECT_EQ(0, Count("foo", eFunctionNameTypeMethod
));
259 // Unmangled with linker annotation
260 EXPECT_EQ(1, Count("puts@GLIBC_2.5", eFunctionNameTypeFull
));
261 EXPECT_EQ(1, Count("puts@GLIBC_2.6", eFunctionNameTypeFull
));
262 EXPECT_EQ(2, Count("puts", eFunctionNameTypeFull
));
263 EXPECT_EQ(2, Count("puts", eFunctionNameTypeBase
));
264 EXPECT_EQ(0, Count("puts", eFunctionNameTypeMethod
));
266 // Itanium mangled with linker annotation
267 EXPECT_EQ(1, Count("_Z5annotv@VERSION3", eFunctionNameTypeFull
));
268 EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeFull
));
269 EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeBase
));
270 EXPECT_EQ(0, Count("annot", eFunctionNameTypeBase
));
271 EXPECT_EQ(0, Count("annot", eFunctionNameTypeMethod
));
273 // Itanium mangled ctor A::A()
274 EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeFull
));
275 EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeBase
));
276 EXPECT_EQ(1, Count("A", eFunctionNameTypeMethod
));
277 EXPECT_EQ(0, Count("A", eFunctionNameTypeBase
));
279 // Itanium mangled dtor A::~A()
280 EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeFull
));
281 EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeBase
));
282 EXPECT_EQ(1, Count("~A", eFunctionNameTypeMethod
));
283 EXPECT_EQ(0, Count("~A", eFunctionNameTypeBase
));
285 // Itanium mangled method A::bar()
286 EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeFull
));
287 EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeBase
));
288 EXPECT_EQ(1, Count("bar", eFunctionNameTypeMethod
));
289 EXPECT_EQ(0, Count("bar", eFunctionNameTypeBase
));
291 // Itanium mangled names that are explicitly excluded from parsing
292 EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull
));
293 EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase
));
294 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod
));
295 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase
));
296 EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull
));
297 EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase
));
298 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod
));
299 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase
));
300 EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeFull
));
301 EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeBase
));
302 EXPECT_EQ(0, Count("Decl", eFunctionNameTypeMethod
));
303 EXPECT_EQ(0, Count("Decl", eFunctionNameTypeBase
));
305 // ObjC mangled static
306 EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeFull
));
307 EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeBase
));
308 EXPECT_EQ(0, Count("ObjCfoo", eFunctionNameTypeMethod
));
310 // ObjC mangled method with category
311 EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeFull
));
312 EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeBase
));
313 EXPECT_EQ(0, Count("ObjCbar", eFunctionNameTypeMethod
));
315 // Invalid things: unable to decode but still possible to find by full name
316 EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeFull
));
317 EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeBase
));
318 EXPECT_EQ(0, Count("_Z12undemangableEvx42", eFunctionNameTypeMethod
));
319 EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeBase
));
320 EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeMethod
));