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