1 | //===-- SymbolTest.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/Mangled.h" |
10 | #include "lldb/Core/DataFileCache.h" |
11 | #include "lldb/Utility/DataEncoder.h" |
12 | #include "lldb/Utility/DataExtractor.h" |
13 | |
14 | #include "gtest/gtest.h" |
15 | |
16 | using namespace lldb; |
17 | using namespace lldb_private; |
18 | |
19 | static void EncodeDecode(const Mangled &object, ByteOrder byte_order) { |
20 | const uint8_t addr_size = 8; |
21 | DataEncoder file(byte_order, addr_size); |
22 | DataEncoder strtab_encoder(byte_order, addr_size); |
23 | ConstStringTable const_strtab; |
24 | |
25 | object.Encode(encoder&: file, strtab&: const_strtab); |
26 | |
27 | llvm::ArrayRef<uint8_t> bytes = file.GetData(); |
28 | DataExtractor data(bytes.data(), bytes.size(), byte_order, addr_size); |
29 | |
30 | const_strtab.Encode(encoder&: strtab_encoder); |
31 | llvm::ArrayRef<uint8_t> strtab_bytes = strtab_encoder.GetData(); |
32 | DataExtractor strtab_data(strtab_bytes.data(), strtab_bytes.size(), |
33 | byte_order, addr_size); |
34 | StringTableReader strtab_reader; |
35 | offset_t strtab_data_offset = 0; |
36 | ASSERT_EQ(strtab_reader.Decode(strtab_data, &strtab_data_offset), true); |
37 | |
38 | Mangled decoded_object; |
39 | offset_t data_offset = 0; |
40 | decoded_object.Decode(data, offset_ptr: &data_offset, strtab: strtab_reader); |
41 | EXPECT_EQ(object, decoded_object); |
42 | } |
43 | |
44 | static void EncodeDecode(const Mangled &object) { |
45 | EncodeDecode(object, byte_order: eByteOrderLittle); |
46 | EncodeDecode(object, byte_order: eByteOrderBig); |
47 | } |
48 | |
49 | TEST(MangledTest, EncodeDecodeMangled) { |
50 | Mangled mangled; |
51 | // Test encoding and decoding an empty mangled object. |
52 | EncodeDecode(object: mangled); |
53 | |
54 | // Test encoding a mangled object that hasn't demangled its name yet. |
55 | mangled.SetMangledName(ConstString("_Z3fooi" )); |
56 | EncodeDecode(object: mangled); |
57 | |
58 | // Test encoding a mangled object that has demangled its name by computing it. |
59 | mangled.GetDemangledName(); |
60 | // EncodeDecode(mangled); |
61 | |
62 | // Test encoding a mangled object that has just a demangled name |
63 | mangled.SetMangledName(ConstString()); |
64 | mangled.SetDemangledName(ConstString("hello" )); |
65 | EncodeDecode(object: mangled); |
66 | |
67 | // Test encoding a mangled name that has both a mangled and demangled name |
68 | // that are not mangled/demangled counterparts of each other. |
69 | mangled.SetMangledName(ConstString("world" )); |
70 | EncodeDecode(object: mangled); |
71 | } |
72 | |