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