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
, BoolConversionOperator
) {
94 ConstString
MangledName("_ZN1a1b1cIiiiEEvm");
95 Mangled
TheMangled(MangledName
);
96 EXPECT_EQ(true, bool(TheMangled
));
97 EXPECT_EQ(false, !TheMangled
);
100 ConstString
UnmangledName("puts");
101 Mangled
TheMangled(UnmangledName
);
102 EXPECT_EQ(true, bool(TheMangled
));
103 EXPECT_EQ(false, !TheMangled
);
106 Mangled TheMangled
{};
107 EXPECT_EQ(false, bool(TheMangled
));
108 EXPECT_EQ(true, !TheMangled
);
112 TEST(MangledTest
, NameIndexes_FindFunctionSymbols
) {
113 SubsystemRAII
<FileSystem
, HostInfo
, ObjectFileELF
, SymbolFileSymtab
>
116 auto ExpectedFile
= TestFile::fromYaml(R
"(
126 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
127 AddressAlign: 0x0000000000000010
131 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
132 Address: 0x0000000000000010
133 AddressAlign: 0x0000000000000010
137 Flags: [ SHF_WRITE, SHF_ALLOC ]
138 Address: 0x00000000000000A8
139 AddressAlign: 0x0000000000000004
144 Section: .anothertext
145 Value: 0x0000000000000045
149 Section: .anothertext
150 Value: 0x0000000000000010
151 Size: 0x000000000000003F
156 Size: 0x000000000000000D
158 - Name: puts@GLIBC_2.5
161 Size: 0x000000000000000D
163 - Name: puts@GLIBC_2.6
166 Size: 0x000000000000000D
168 - Name: _Z5annotv@VERSION3
171 Size: 0x000000000000000D
176 Size: 0x000000000000000D
181 Size: 0x000000000000000D
186 Size: 0x000000000000000D
188 - Name: _ZGVZN4llvm4dbgsEvE7thestrm
191 Size: 0x000000000000000D
193 - Name: _ZZN4llvm4dbgsEvE7thestrm
196 Size: 0x000000000000000D
198 - Name: _ZTVN5clang4DeclE
201 Size: 0x000000000000000D
206 Size: 0x000000000000000D
208 - Name: +[B ObjCbar(WithCategory)]
211 Size: 0x000000000000000D
213 - Name: _Z12undemangableEvx42
216 Size: 0x000000000000000D
220 ASSERT_THAT_EXPECTED(ExpectedFile
, llvm::Succeeded());
222 auto M
= std::make_shared
<Module
>(ExpectedFile
->moduleSpec());
224 auto Count
= [M
](const char *Name
, FunctionNameType Type
) -> int {
225 SymbolContextList SymList
;
226 M
->FindFunctionSymbols(ConstString(Name
), Type
, SymList
);
227 return SymList
.GetSize();
231 EXPECT_EQ(1, Count("main", eFunctionNameTypeFull
));
232 EXPECT_EQ(1, Count("main", eFunctionNameTypeBase
));
233 EXPECT_EQ(0, Count("main", eFunctionNameTypeMethod
));
236 EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeFull
));
237 EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeBase
));
238 EXPECT_EQ(1, Count("foo", eFunctionNameTypeBase
));
239 EXPECT_EQ(0, Count("foo", eFunctionNameTypeMethod
));
241 // Unmangled with linker annotation
242 EXPECT_EQ(1, Count("puts@GLIBC_2.5", eFunctionNameTypeFull
));
243 EXPECT_EQ(1, Count("puts@GLIBC_2.6", eFunctionNameTypeFull
));
244 EXPECT_EQ(2, Count("puts", eFunctionNameTypeFull
));
245 EXPECT_EQ(2, Count("puts", eFunctionNameTypeBase
));
246 EXPECT_EQ(0, Count("puts", eFunctionNameTypeMethod
));
248 // Itanium mangled with linker annotation
249 EXPECT_EQ(1, Count("_Z5annotv@VERSION3", eFunctionNameTypeFull
));
250 EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeFull
));
251 EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeBase
));
252 EXPECT_EQ(0, Count("annot", eFunctionNameTypeBase
));
253 EXPECT_EQ(0, Count("annot", eFunctionNameTypeMethod
));
255 // Itanium mangled ctor A::A()
256 EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeFull
));
257 EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeBase
));
258 EXPECT_EQ(1, Count("A", eFunctionNameTypeMethod
));
259 EXPECT_EQ(0, Count("A", eFunctionNameTypeBase
));
261 // Itanium mangled dtor A::~A()
262 EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeFull
));
263 EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeBase
));
264 EXPECT_EQ(1, Count("~A", eFunctionNameTypeMethod
));
265 EXPECT_EQ(0, Count("~A", eFunctionNameTypeBase
));
267 // Itanium mangled method A::bar()
268 EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeFull
));
269 EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeBase
));
270 EXPECT_EQ(1, Count("bar", eFunctionNameTypeMethod
));
271 EXPECT_EQ(0, Count("bar", eFunctionNameTypeBase
));
273 // Itanium mangled names that are explicitly excluded from parsing
274 EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull
));
275 EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase
));
276 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod
));
277 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase
));
278 EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull
));
279 EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase
));
280 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod
));
281 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase
));
282 EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeFull
));
283 EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeBase
));
284 EXPECT_EQ(0, Count("Decl", eFunctionNameTypeMethod
));
285 EXPECT_EQ(0, Count("Decl", eFunctionNameTypeBase
));
287 // ObjC mangled static
288 EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeFull
));
289 EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeBase
));
290 EXPECT_EQ(0, Count("ObjCfoo", eFunctionNameTypeMethod
));
292 // ObjC mangled method with category
293 EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeFull
));
294 EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeBase
));
295 EXPECT_EQ(0, Count("ObjCbar", eFunctionNameTypeMethod
));
297 // Invalid things: unable to decode but still possible to find by full name
298 EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeFull
));
299 EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeBase
));
300 EXPECT_EQ(0, Count("_Z12undemangableEvx42", eFunctionNameTypeMethod
));
301 EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeBase
));
302 EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeMethod
));