1 //===- unittests/Support/SymbolRemappingReaderTest.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 "llvm/Support/SymbolRemappingReader.h"
10 #include "llvm/Support/MemoryBuffer.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 
15 namespace {
16 class SymbolRemappingReaderTest : public testing::Test {
17 public:
18   std::unique_ptr<MemoryBuffer> Buffer;
19   SymbolRemappingReader Reader;
20 
readWithErrors(StringRef Text,StringRef BufferName)21   std::string readWithErrors(StringRef Text, StringRef BufferName) {
22     Buffer = MemoryBuffer::getMemBuffer(Text, BufferName);
23     Error E = Reader.read(*Buffer);
24     EXPECT_TRUE((bool)E);
25     return toString(std::move(E));
26   }
27 
read(StringRef Text,StringRef BufferName)28   void read(StringRef Text, StringRef BufferName) {
29     Buffer = MemoryBuffer::getMemBuffer(Text, BufferName);
30     Error E = Reader.read(*Buffer);
31     EXPECT_FALSE((bool)E);
32   }
33 };
34 } // unnamed namespace
35 
TEST_F(SymbolRemappingReaderTest,ParseErrors)36 TEST_F(SymbolRemappingReaderTest, ParseErrors) {
37   EXPECT_EQ(readWithErrors("error", "foo.map"),
38             "foo.map:1: Expected 'kind mangled_name mangled_name', "
39             "found 'error'");
40 
41   EXPECT_EQ(readWithErrors("error m1 m2", "foo.map"),
42             "foo.map:1: Invalid kind, expected 'name', 'type', or 'encoding', "
43             "found 'error'");
44 }
45 
TEST_F(SymbolRemappingReaderTest,DemanglingErrors)46 TEST_F(SymbolRemappingReaderTest, DemanglingErrors) {
47   EXPECT_EQ(readWithErrors("type i banana", "foo.map"),
48             "foo.map:1: Could not demangle 'banana' as a <type>; "
49             "invalid mangling?");
50   EXPECT_EQ(readWithErrors("name i 1X", "foo.map"),
51             "foo.map:1: Could not demangle 'i' as a <name>; "
52             "invalid mangling?");
53   EXPECT_EQ(readWithErrors("name 1X 1fv", "foo.map"),
54             "foo.map:1: Could not demangle '1fv' as a <name>; "
55             "invalid mangling?");
56   EXPECT_EQ(readWithErrors("encoding 1fv 1f1gE", "foo.map"),
57             "foo.map:1: Could not demangle '1f1gE' as a <encoding>; "
58             "invalid mangling?");
59 }
60 
TEST_F(SymbolRemappingReaderTest,BadMappingOrder)61 TEST_F(SymbolRemappingReaderTest, BadMappingOrder) {
62   StringRef Map = R"(
63     # N::foo == M::bar
64     name N1N3fooE N1M3barE
65 
66     # N:: == M::
67     name 1N 1M
68   )";
69   EXPECT_EQ(readWithErrors(Map, "foo.map"),
70             "foo.map:6: Manglings '1N' and '1M' have both been used in prior "
71             "remappings. Move this remapping earlier in the file.");
72 }
73 
TEST_F(SymbolRemappingReaderTest,RemappingsAdded)74 TEST_F(SymbolRemappingReaderTest, RemappingsAdded) {
75   StringRef Map = R"(
76     # A::foo == B::bar
77     name N1A3fooE N1B3barE
78 
79     # int == long
80     type i l
81 
82     # void f<int>() = void g<int>()
83     encoding 1fIiEvv 1gIiEvv
84   )";
85 
86   read(Map, "foo.map");
87   auto Key = Reader.insert("_ZN1B3bar3bazIiEEvv");
88   EXPECT_NE(Key, SymbolRemappingReader::Key());
89   EXPECT_EQ(Key, Reader.lookup("_ZN1A3foo3bazIlEEvv"));
90   EXPECT_NE(Key, Reader.lookup("_ZN1C3foo3bazIlEEvv"));
91 
92   Key = Reader.insert("_Z1fIiEvv");
93   EXPECT_NE(Key, SymbolRemappingReader::Key());
94   EXPECT_EQ(Key, Reader.lookup("_Z1gIlEvv"));
95 }
96