96 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/ProfileData/SymbolRemappingReader.h"10#include "llvm/Support/MemoryBuffer.h"11#include "gtest/gtest.h"12 13using namespace llvm;14 15namespace {16class SymbolRemappingReaderTest : public testing::Test {17public:18 std::unique_ptr<MemoryBuffer> Buffer;19 SymbolRemappingReader Reader;20 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 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 namespace35 36TEST_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 46TEST_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 61TEST_F(SymbolRemappingReaderTest, BadMappingOrder) {62 StringRef Map = R"(63 # N::foo == M::bar64 name N1N3fooE N1M3barE65 66 # N:: == M::67 name 1N 1M68 )";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 74TEST_F(SymbolRemappingReaderTest, RemappingsAdded) {75 StringRef Map = R"(76 # A::foo == B::bar77 name N1A3fooE N1B3barE78 79 # int == long80 type i l81 82 # void f<int>() = void g<int>()83 encoding 1fIiEvv 1gIiEvv84 )";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